Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Running an application with arguments through applescript...

Hi there,


I'm just starting to get my head around AppleScript and I was hoping someone could help me get started on this.

I'm just trying to launch an application with arguments and im having a little trouble.

Firstly I'm not sure how I'd get the actuall path to the application to run...

(Also I'm not sure how you would apply the arguments but just getting it to run would be nice as a start.)


If anyone could help, it would be great!


Thanks,

Pete

macbook pro

Posted on Feb 15, 2013 10:06 PM

Reply
Question marked as Best reply

Posted on Feb 16, 2013 2:31 AM

You don't find the path as such, you refer to it by its bundle identifier.


In Applescript, first use


get id of application "name"


where you replace name with the app's common name. In the results of the editor you'll get something like


"com.something.name"


(you may get asked to locate the app visually first through a Finder-like window). That's the bundle identifier. You then use that with 'id' in your commands instead of a path, like


tell application id "com.something.name" to activate

16 replies
Question marked as Best reply

Feb 16, 2013 2:31 AM in response to peterleary

You don't find the path as such, you refer to it by its bundle identifier.


In Applescript, first use


get id of application "name"


where you replace name with the app's common name. In the results of the editor you'll get something like


"com.something.name"


(you may get asked to locate the app visually first through a Finder-like window). That's the bundle identifier. You then use that with 'id' in your commands instead of a path, like


tell application id "com.something.name" to activate

Feb 16, 2013 6:21 PM in response to softwater

Hey Softwater, thanks for that!


I originally managed to get it going with this -

tellapplication "/Applications/Sublime Text 2.app" toactivate


but now im using your suggestion and this -

tell application id "com.sublimetext.2" to activate

It works so much better because it's not dependent on paths. Thanks for that! Just wondering is it possible to incorperate arguments with this method?


Thanks,

Pete


Feb 16, 2013 6:30 PM in response to peterleary

You can actually just say:


tellapplication "Sublime Text 2" toactivate


as long as the app has been launched once


To pass arguments to a script you need have an explicit run handler that looks like this:


on runarguments


-- main part of script goes in here

end run


the arguments variable will contain anything you pass to the app as parameters.


I'll point out that you can also call script app handlers directly. for instance, if your script has a handler called myHandler(myParameter) , you call it externally from applescript by using


tell application "Sublime Text 2.app"


myHandler(someData)

end tell

Feb 16, 2013 6:39 PM in response to peterleary

Is this app Sublime Text 2 the text editor? If it is while you can run it form an Applescript to interact with it form Applescript the app developer would have had to have added those hooks into the app.


To see if the developer did this open Applescript and do File->Open Dictionary and look to see if Sublime Text 2 is listed. If it is then you can find the calls you can make to the app to have it do things. If it is not listed while it is still possible to script the app it will be a lot more difficult.

Feb 16, 2013 6:41 PM in response to twtwtw

twtwtw wrote:


You can actually just say:


tellapplication "Sublime Text 2" toactivate


as long as the app has been launched once



Absolutely. Using the bundle identifier is good practice if you're scripting a lot, passing scripts to others, or want to be sure your scripts are as future proof as possible, i.e., it's good commercial practice. But if you're just knocking up a utility for your own personal use, any way that works is good. 🙂

Feb 17, 2013 5:15 AM in response to VikingOSX

VikingOSX wrote:


2. There is no man page for subl. It is a soft link from within the app package


Sublime Text 2 Documentation


OS X Command Line


Sublime Text 2 includes a command line tool, subl, to work with files on the command line


Setup

The first task is to make a symlink to subl. Assuming you've placed Sublime Text 2 in the Applications folder, and that you have a ~/bin directory in your path, you can run:

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl



Usage

Run subl --help




If you're going to use an application you should at least read the documentation

Feb 17, 2013 5:35 AM in response to Frank Caggiano

Frank,


A veteran UNIX user understands that “man” pages are installed on the user's machine and accessible via the terminal man(1) command, or purpose built GUI. These pages are written in troff syntax. Documentation on the other hand, and in particular, the Sublime Text 2 documentation that you link too, is not man pages. That was my point.


I am very familiar with Sublime Text 2 and the documentation that you link too. One of the first things that I did upon installation, and after installing Package Control, was to do the very same symlink as you point out.


Oh, by the way, I have very thoroughly read the Sublime Text 2 documentation, and took the effort to learn a good deal more about it.

Feb 17, 2013 8:23 AM in response to Frank Caggiano

Technically, you can't really argue with Viking. There is no man page. Doing


man subl


produces an error. As you note yourself, the usage is


subl --help


But either way, I'd just like to thank you for arguing about it. I've been using Tincta and recently Tincta Pro as my code editor and had never heard of subl till this thread. After seeing the passion here, I downloaded it to see what all the fuss was about.


Guess I won't be needing Tincta anymore! Thanks for the tip guys. 🙂

Feb 18, 2013 2:45 AM in response to peterleary

Wow, thanks guys. I woke up and there were posts everywhere!


So I created the subl symlink it using

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl


Now my applescript looks like this - do shell script "/Users/Pete/bin/subl /Users/Pete/bin/test.txt"

And that seems to work great for a start


Thanks again for all the help! Glad you got into it softwater 🙂

Pete

Running an application with arguments through applescript...

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.