What applescript will a particular application accept? - macos

I see a lot of applications that can respond to applescript messages such as this one.
tell application "Safari"
return URL of front document as string
end tell
How can I query an application to find out what commands it will accept? Is there a tool for doing so?

In AppleScript Editor, do "Open Dictionary", select the application you are interested in, and you get a nice browser that shows the commands and their documentation (if the developers have chosen to provide any).

Related

Expected end of line but found “"”. is showing up on apple script editor

tell application "Finder" to say "this is a test"
tell aplication"finder"
activate
repeat 5 times
make new Finder window
end repeat
end tell
I am just learning different coding and know quite a bit about html, css, and javascript. I am completely new to apple script editor.
Since you're new to applescript I'll give you a basic tip to learn. Only tell an application to do something that it knows how to do. Each application knows how to do specific things and applescript knows how to do things by itself too.
I tell you this because the "say" command is an applescript command, not a Finder command. So there's no reason to tell the Finder to say anything. As you get more complex in your scripts you will find errors if you tell the wrong application to do something. As such you can run the say command by itself. Try this and it will work by itself...
say "this is a test"
The easiest way to know what each application understands is to look in the dictionaries. In Script Editor, under the file menu choose "open dictionary". You can choose any application but for this example open the Finder dictionary. You can search through it to find what the Finder knows how to do. You'll notice it doesn't have the "say" command thus you know not to tell the Finder to use the say command. You can type "say" into the search field and you'll see it doesn't return any results.
If you open the dictionary for "StandardAdditions" you'll find say in there. That's additional things applescript knows by itself.
Good luck.
The part tell aplication"finder" has two typos and is missing a space. It should be tell application "Finder"
The whole think should look like this which builds for me.
tell application "Finder" to say "this is a test"
tell application "Finder"
activate
repeat 5 times
make new Finder window
end repeat
end tell

Can I manipulate the location of a dialog displayed through osascript?

I've been playing around with various UNIX commands and came across this one to display a dialog:
osascript -e 'tell app "System Events" to display dialog "Hello World"'
I'd like to change the position of the dialog. For most commands, I can just use man command to figure out how to do something for that command. However, man osascript doesn't tell me how I can change the position of the dialog box. How can I modify the command to put the dialog in a different place?
First, to get help with applescript just open the AppleScript Editor application and look under the help menu. The applescript language guide is in there and other tools. Also under the file menu is "Open Dictionary" which will show you the specific commands for applications.
To see the information about display dialog, open the dictionary of StandardAdditions and use the search field to find the command.
To answer your question, no, you cannot specify the location of a "display dialog" window. There are no parameters in that command for positioning the window, as you'll see in the dictionary. In addition, no other commands will help you either because you can't issue other commands while the dialog window is open because the code pauses while the window is open waiting for a response from the dialog window (like when you press the OK button).
If you need to set the position of a window to display information to a user then you'll need to use some other dialog tool other than "display dialog". You could create your own window in cocoa or google for some alternatives like cocoaDialog, SKProgressBar, and Notification Center.
There is a round-about way to go about this, which may be useful in some scenarios. Try the following:
on displayMessage(msg)
tell application "Finder"
activate
set c to (count windows)
ignoring application responses
display dialog msg with icon note
end ignoring
end tell
tell application "System Events"
tell application process "Finder"
repeat until ((count windows) > c)
delay 0.2
end repeat
set position of window 1 to {0, 22}
end tell
end tell
end displayMessage
displayMessage("I'm over here!")
Credit for this little script goes to a post here.
In implimenting this myself, I found it was limited to whether or not the application that is being called (Finder, in the example) supports the count window command (or whether it has API support at all).
I realise I've dragged up a question from 2013. I am posting this answer here in case it is of use to the OP or, more likely, someone else with a similar question.

Increase speed of applescript keystroke

I'm using applescript to automate some browser activity. I have it tied to the speech recognition, so that I can make some custom voice commands.
One such command is "I wanna go home", which when heard, pulls up the relevant bus schedule on my browser. Given that the public transit system uses PHP and GET, the URL gets pretty long. Therefore, the keystroke myURL in the applescript takes a while to execute (something like 1-2 seconds). While I can live with losing 1-2 seconds, I'd really rather not.
With that in mind, is it possible to send these keystrokes faster? I believe I read somewhere that using key code is faster than using keystroke, but for the life of me, I can't figure out where I read that.
EDIT: My browser of choice is Google Chrome, which I couldn't find URL hooks for. This is why I had to resort to using keystrokes. Therefore, I'd prefer answers that work with Chrome over Safari
Another way to solve this is to put the text in the clipboard, then paste it. You can save the clipboard contents first and put it back afterward, so you don't lose what's already there. This method works in other situations when you want to enter a lot of text, not just for browser URLs like the other answers.
set clipboard_contents to (the clipboard)
set the clipboard to "Some long string that would take a while with the keystroke method"
tell application "System Events" to keystroke "v" using command down
delay 0.2 -- needed because otherwise the next command can run before the paste occurs
set the clipboard to clipboard_contents
I'm pretty sure you can script your browser to open the URL directly, instead of typing in the keystrokes (faking input events in AppleScript should be considered a last resort).
You can do this with the open location standard addition:
open location "http://google.com"
Open location is defined in Standard Additions and should not be enclosed in a tell statement.
open location "http://google.com"
If you want to target a specific browser you can use:
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "http://google.com"
end tell
To target Chrome:
tell application "Google Chrome"
if not (exists window 1) then reopen
set URL of active tab of window 1 to "https://www.google.com/"
end tell

Applescript API documentation

I want to make an AppleScript to automate the task of switching resolution on the MacBook Pro Retina.
Searching the internet for "applescript system preferences" I came across a page where some preferences are show. Being the scaled resolution thing new, it is not documented.
This brings to a bigger problem I have with AppleScript (mind that apart from copy-pasting something I never really programmed in it). Where is the documentation that tells me, for instance, tha the System Preferences object is actually called "System Preferences", that it has objects called "pane", that they have an id and that the expose id is "com.apple.preference.expose"?
It seems like there must be some sort of "secret" documentation for every program, and they must be huge, mapping all the object hierarchies and possible actions. In the end, AppleScript core is minimal and all you do is manipulate such programs. But where are they documented?
Ok this is how it works:
Where is the documentation that tells me, for instance, tha the System Preferences object is actually called "System Preferences"
The object is called "System Preferences" because that is the exact name of the application. What you're telling Applescript with this is I want to speak to the application named System Preferences (tell application "System Preferences" ...)
that it has objects called "pane"
Now it's the fun part. If you open your Library window (in Applescript Editor, Window > Library) you will see that there is a collection of scriptable applications available, the thing is that 'System Preferences' is not there. So let's find it: File > Open Dictionary > System Preferences. Now you got a window that both lets you drill down all available classes/commands/properties of the app and also a split window with relevant documentation (if you click on SSystem Preferences you'll see Cpane and by clicking on this you'll see Pid among others). The id of the pane for once more would be the name of the pane (lowercased and concatenated - I'm still looking into documentation for a strict definition on this). I hope that this will get you started.
S:Suite
C:Class
P:Property
(the 'C' inside a circle stands for Command)
You're exactly right. Each program does have its own documentation for applescript. It's called its applescript dictionary. You can see the dictionary of any application by any of the following...
1) in AppleScript Editor, under the File menu, choose "Open Dictionary...". You can select an application from there and it will show its dictionary.
2) drag/drop an application onto the AppleScript Editor's icon.
3) there's a list of frequently-used dictionaries for fast access. Under the Window menu in AppleScript Editor choose "Library". You can double-click an application in that list. You can also modify that list to contain dictionaries that you want in the list.
Good luck.
You can ask AppleScript to tell you the ids for each of the panes.
tell application "System Preferences" to get the id of every pane
This is particuarly handy as it will tell you the ids for any third-party preference panes you have installed. For instance, I was able to work out that the pane for my Microsoft Natural keyboard is called com.microsoft.microsoftkeyboard
I haven't really explored this much yet, but I would expect that similar syntax exists to identify the objects within any scriptable application.
I have another issue but you can have a look to my question as there is some hint in my script about your issue
HOW TO: display a check mark, disable a menu item, refresh a menubar
For instance:
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell
This code should directly put you in the resolution preferences of the system preference.
Then you can make a code to recuperate all the UI elements of the pane so that you now which action to trigger. Something like this should also work:
tell application "System Events"
tell application process "System Preferences"
set frontmost to true
delay 1
return every UI element of front window
return name of every UI element of front window
end tell
end tell
Hope it helps

Have one application execute an action when another application is given a command using AppleScript?

I know nothing about AppleScript, but I wonder if it could make my life easier: is there a way to write an AppleScript that tells Safari / Firefox / Chrome to refresh the current tab when I save a document in another application, say TextWrangler? Essentially, I want to map the Command+S keyboard shortcut to do two things at once in two separate applications.
If that’s not possible, can you script one application so that saving one file executes a command in another window in that same application?
There are different possible approaches to implement this, but the most straightforward would probably be to create a script that executes all steps you need (i.e. save the document and refresh the window) and bind that to the Cmd+S keyboard combo in the triggering application.
What you need for this approach to work, is, in order:
a method to bind a key combo to a script effective only in a specific application. OS X’ Automator Services fit that bill: their scope can be restricted to a single application (select it in the “only in” drop down at the top of the workflow actions), and they can be assigned a shortcut in the Keyboard preference pane of System Preferences.
a way to relay your commands to the applications they target. AppleScript can help you in two different ways, depending on the fact if your applications are scriptable, i.e. if they have a scripting dictionary you can inspect in the AppleScript editor:
if they do, and their terminology includes the saving action for the editor on one side (most scriptable document based apps do so in the form save <document>), the page refreshing for the browser(s) on the other (Chrome has reload <tab>, Safari gets the same result via a JavaScript detour, i.e. do JavaScript "window.location.reload()" in <document> – I don’t use Firefox), you are set.
if they do not, GUI Scripting might help, i.e. simulating a click on the right UI element (menu or toolbar) via tell application "System Events" to tell process <your process> to click item x of menu y.
That script can then be embedded into the Automator workflow (in an “Run AppleScript” action, to be precise).
As you can see, a lot depends on the exact software you are using. if you are new to AppleScript and the above baffles you, I’d suggest spending a bit of time on the AppleScript pages of Mac OS X Automation (where you’ll also find example scripts which will kick start you into things like GUI Scripting).
One final note: as of this writing, sandboxed applications do not honor key combos assigned to them in the Keyboard Preference pane (they do honor global key combos set there – just not those specifically targeting them). This means you cannot, for instance, currently override TextEdit’s Cmd+S shortcut for saving in Lion. As long as your editor is not sandboxed (easily checked in Activity Monitor), you should have no issue with this.
One solution would be to create a folder action to refresh the current tab when a new file is saved in the folder.
on adding folder items to theFolder after receiving theFiles
tell application "Google Chrome"
activate
tell window 1 to tell active tab
set URL to (get URL)
end tell
end tell
end adding folder items to

Resources