Programmatically Create Apple Script

梦想与她 提交于 2019-12-11 09:45:41

问题


I am using privelaged javascript which has access to python like ctypes and the whole mac api.

Programmatically I'm trying to create an applescript file and set its icon.

I'm trying follow this tutorial here on how to make profile shortcuts on macs: http://weblogs.mozillazine.org/asa/archives/2008/08/shortcut_to_lau.html

Is this as simple as creating a text file and populating it with some text?

I dont have a mac, just coding for my mac users.


回答1:


You'll want to use the command line tool osacompile to create a compiled applescript file. You can look at its man page to see all your options but it's rather simple. For example, suppose you wanted to write the following applescript code to a file...

tell application "Safari"
    activate
end tell

You can do it with osacompile and using the "-e" option before each line of code. Note that I put single quotes around each line too.

set savePath to (path to desktop as text) & "test.scpt"

do shell script "osacompile -e 'tell application \"Safari\"' -e 'activate' -e 'end tell' -o " & quoted form of POSIX path of savePath

If you don't like the "-e" option, you could pipe the code text using echo...

set scriptText to "tell application \"Safari\"
activate
end tell"

set savePath to (path to desktop as text) & "test.scpt"

do shell script "echo " & quoted form of scriptText & " | osacompile -o " & quoted form of POSIX path of savePath

The saved file will have the default applescript icon. If you also want to change its icon you can use a command line tool I wrote called SetFileIcon. Find it here. There's directions on the page.



来源:https://stackoverflow.com/questions/25817752/programmatically-create-apple-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!