William Lombard
William Lombard

Reputation: 347

Python 2 and AppleScript. Dialogue prompts and default answers

Here is just a small snippet of the Python code I use to read the XML data of Mac System Profiler files for the purpose of adding the data to a database without having to copy/paste the details manually.

Specifically this snippet of code prompts the user to enter an "Article Number" to save the given ".spx" (System Profiler Report) under.

import subprocess

exampleString = "12345"

theText = subprocess.check_output(['osascript', '-e',
                              r'''set theText to text returned of (display dialog "Enter new Article Number here:
                               \n\nElse just press OK" default answer "" with title "Hardware Paster v1.0" with icon 2)'''
                              ])

"theText" will go to on dictate the name of the spx file.

I would like to set the value of the "default answer" inside of the AppleScript prompt with the value of another variable inside the main python script ("exampleString" in this case). The "12345" is just placeholder text here.

The ultimate aim is to minimise data-entry by the end user.

Thanks for your help.

Upvotes: 0

Views: 319

Answers (1)

ddg
ddg

Reputation: 1098

Just string format and you're good!

>>> exampleString = "world"
>>> 'hello there "{}"!'.format(exampleString)
'hello there "world"!'

And applied to your program:

exampleString = "12345"

template = r'''set theText to text returned of (display dialog "Enter new Article Number here:
\n\nElse just press OK" default answer "{}" with title "Hardware Paster v1.0" with icon 2)'''

theText = subprocess.check_output(
    ['osascript', '-e', template.format(exampleString)])

Upvotes: 1

Related Questions