pass and receive values between python script and applescript

后端 未结 2 1374
走了就别回头了
走了就别回头了 2021-01-24 06:14

I\'m very new to python and applescript. I have a python script which is calling 2 applescripts. I would like to define few global variables in python and pass to applescript 1

2条回答
  •  独厮守ぢ
    2021-01-24 06:18

    You have to return something from the "on run" handler in applescript otherwise the returned result is only the result of the last line of code. So you'll want to do something like this...

    on run argv
        set returnList to {}
    
        if (item 1 of argv starts with "x") then
            set end of returnList to function1(item1 of argv)
        else
            set end of returnList to function2(item 1 of argv)
            set end of returnList to function3(item 2 of argv)
        end if
    
        return returnList
    end run
    

    Also your functions will need to look something like this if you want the user to supply something. Note that I'm telling the Finder to show the dialog. That's because you are running this from python and it will error if some application doesn't handle the user interaction.

    on function1(var)
        tell application "Finder"
            activate
            set var to text returned of (display dialog "Enter a value" default answer "")
        end tell
        return var
    end function1
    

提交回复
热议问题