Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open programs with applescript

Tags:

applescript

2 part question:

I'm simply trying to run programs using applescript from the terminal, so I tried:

$ osascript tell application "iTunes" to activate 

and get the error:

osascript: tell: No such file or directory 

Giving the full path to the program did not work either. What am I missing? The second part of the question is what I eventually want to use applescript for. I would like to use it to open an application I built using py2app. Can applescript open any mac app or just certain ones that are already compatible.

Thanks

like image 428
sabajt Avatar asked Apr 05 '12 03:04

sabajt


People also ask

How do I open an AppleScript File?

In Script Editor, you can save your script as an app. To run the script, just open it in the Finder, just like any other app. With a script open in the Script Editor app on your Mac, choose File > Export. Enter a name for the app.

Does Apple still support AppleScript?

However, iOS and iPadOS have no support for AppleScript, and Apple is clearly heading in a trend of switching many apps over to Catalyst (and strongly encouraging 3rd party apps to do the same).

How do I run AppleScript automatically?

Click Actions in the top-left corner of the Automator window, then select Utilities in the Library. Drag the Run AppleScript action into your workflow. You can edit, compile, and test your script right in the action, or you can develop your script in Script Editor.


2 Answers

Try this. Notice you use "-e" when you are writing the command. Without "-e" you would give a path to an applescript to run. Also notice the string command must be in quotes.

osascript -e "tell application \"iTunes\" to activate" 

And if you have a multi-line applescript you use "-e" before each line like this...

osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell" 

If you want to open an application just use the unix "open" command...

open "/path/to/application" 

If you wanted to open an application using applescript and the "activate" command doesn't work (it should work for almost everything though) then tell the Finder to open it. Remember that applescript uses colon delimited paths...

osascript -e "tell application \"Finder\" to open file \"path:to:application\"" 
like image 190
regulus6633 Avatar answered Sep 20 '22 15:09

regulus6633


In a bash shell (like in Terminal), you can send multiple lines to osascript by using a "here document".

osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell" 

becomes

osascript <<EOF tell application "iTunes"    activate end tell EOF 

As an old-skool Unix hacker, I save these little snippets in my $HOME/bin directory and call them from the command line. Still learning the particulars, though.

Alan

like image 24
Alan Porter Avatar answered Sep 24 '22 15:09

Alan Porter