applescript oneliner

和自甴很熟 提交于 2021-01-01 06:37:45

问题


Is it possible to join applescript lines into one (in ruby it can be done using ;)?


回答1:


Not really. The most that can be done is to take a simple if-then statement and make it into one line...

if (variable) then 
    return true
end if

...becomes...

if (variable) then return true

If you were to include the osascript command in a shell script, then multiple line scripts must delimited with -e...

osascript -e 'if (variable) then' -e 'return true' -e 'end if'

But that's about the extent of it. Applescript files aren't straightforward text files like most other programming languages (unfortunately) and we have to rely on its specialized editors for line management.




回答2:


It depends on your code.

When you use AppleScript for GUI scripting, you can often write a bunch of nested tell blocks as one line.

For example these nested tell blocks:

tell application "System Preferences"
    activate
end tell

tell application "System Events"
    tell application process "System Preferences"
        tell window "System Preferences"
            tell scroll area 1
                tell button "General"
                    click
                end tell
            end tell
        end tell
    end tell
end tell

They could also be written as:

tell application "System Preferences" to activate
tell application "System Events" to tell application process "System Preferences" to tell window "System Preferences" to tell scroll area 1 to tell button "General" to click



回答3:


If you really want to avoid -e and force everything on one line you can push everything through echo

osascript -e "`echo -e \"tell application \\"MyApp\\"\nactivate\nend tell\"`"

Where " become \\" and newlines become \n.




回答4:


I have re-arranged AppleScript from a block format, to a single line format as such:

Block format

tell application <application>
  activate
  open location <url>
end tell

Single line format

osascript -e "tell application \"<application>\" to activate & open location \"<url>\"";



来源:https://stackoverflow.com/questions/4108808/applescript-oneliner

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