0

I have a Python script that evaluates certain directories on my hard-drive for criteria matching files. It returns a list of strings, containing the file names.
In Applescript, I call the Python script with do shell script "path-to-script.py".
If I print the list inside the Python script and run the script from Applescript, it appears as a single string (i.e. "['string1', 'string2', ..., 'stringX']") in the results dialog of the Script Editor App.

Does anybody know, how to pass this Python list to an identical Applescript list?

Thanks.

1

2 Answers 2

1

The most straightforward way to do this is to have the Python script print the list items on separate lines:

print("\n".join(list_of_files))

And in AppleScript, break those lines up into a list:

set AppleScript's text item delimiters to return
set list_of_files to text items of (do shell script "path-to-script.py")
Sign up to request clarification or add additional context in comments.

Comments

0

It would be good practice to save Applescript's text item delimiters to a variable first and restore them after you have done what you want. Also enclose it all in a try block to avoid leaving the text delimiters in an unexpected state for any other scripts that may be run. i.e.

try 
    set oldDelims to AppleScript's text item delimiters 
    -- do your stuff
    --restore default delimiters:
    set AppleScript's text item delimiters to oldDelims
on error
    set AppleScript's text item delimiters to oldDelims
end try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.