Given an object in Applescript (i.e. I have an Object Specifier to it), how do I find out what all accessible properties(?) that it has are? I am not sure that properties is the right word here, and part of my question is what the correct vocabulary is; I will illustrate what I mean with the following example.
For instance:
Using the following Object Specifier, tell application "System Events" to get process "TextEdit", I can access the window property(?) of it, such as tell application "System Events" to get window of process "TextEdit". (Note that window is a list, apparently).
What I want is to query the Object Specifier in some way that will show me which things (properties?), like window, are available in the object. In Python, for example, this could be done using dir(object_name). Is there an equivalent in Applescript?
I've tried the following two approaches but these, while providing useful lists, do not achieve the goal since they do not return window like desired in the example.
tell application "System Events" to get properties of process "TextEdit"
tell application "System Events" to get attributes of process "TextEdit"
I am looking for something in a similar style which would return window and the other properties(?) like window. How can I call such a query in Applescript code?
Also, what precisely is the window of process "TextEdit" here? Is it a property? An attribute? Something else? Is there an easy way to answer that question within the Applescript Editor, such as perhaps by interpreting the blue-italics display of the returned window object? Does that appearance have significance for distinguishing between properties, attributes, etc? (What does 'etc' include here, specifically and using correct vocabulary, is part of my question..)
In AppleScript
Properties are leaf items like name, identifier.
Elements are node items like attributes, windows, documents.
However element is not a part of the standard AppleScript terminology.
If the application is scriptable (which TextEdit is) you get a scripting dictionary to access the application elements and properties.
If the application is not scriptable you can access the UI properties and elements of the application process via System Events.
The AppleScript dictionary of an application contains all information about the available elements, properties, commands / events, enumerations and value types. In Script Editor press ⇧⌘O and select the dictionary.
A more sophisticated – and expensive – tool is Script Debugger which provides a terminology browser. For professional script developing it's indispensable.
Related
Very often I want to inspect value of an object or its properties, here's a trivial example:
tell application "System Events"
set procList to a reference to (every process)
repeat with proc in procList
-- I'd like to see `properties of proc` but how do I do that?
end repeat
end tell
I can't use display dialog, can I?. I tried, but couldn't make it work.
Maybe it's possible to do something like "tell application TextEdit ..." and dump all that info into an "Untitled" file window?
How do you guys inspect complex objects?
Most objects in AppleScript support the properties property, which returns an AppleScript record containing the properties of that object. The easiest way to inspect that is to use the logging feature of Script Editor. In other words, run this code:
tell application "System Events"
set procList to a reference to (every process)
repeat with proc in procList
log (get properties of proc)
end repeat
end tell
... then go to the log section in Script Editor and look at the properties. Sometimes the log command isn't even necessary; some objects dump the value of their properties into the log on retrieval.
Script Debugger allows you to browse “AppleScriptable” applications’ dictionaries and live state. That’s really your only option. Try the free version and see how you get on.
I need to be able to run the script through Terminal only. I've seen other scripts that work as long as you change some settings in Accessibility; this is not an option for what I'm trying to do. I've tried the script below, but receive the following error:
0:13: script error: A real number can’t go after this identifier. (-2740)
tell application "System Events"
set dockPlistFile to property list file "~/Library/Preferences/com.apple.dock.plist"
tell dockPlistFile
tell property list item "persistent-apps"
set appTileItems to value of (every property list item whose value of property list item "tile-data"'s property list item "file-label" is not "Terminal")
set its value to appTileItems
end tell
end tell
end tell
tell application "Dock" to quit
I'm trying to get rid of the Terminal icon from the dock. How can I do this correctly?
I think this ask different answer will help you run a dock modification without changes to Accessibility settings.
Basically you'll chain a launch agent XML file to a shell script and call your apple script from within that.
I can't manage to find how to use the selected text as a variable for AppleScript and Automator.
Any ideas?
For Applescript, it works with other applications. To get the selected text of the front window in an app, Applescript has to use the language/syntax that this app understands/responds to. For very scriptable, text document based apps, there is much similarity, looking something like:
tell app "xyz" to get selection of document 1
However, there really is no standard. Many apps don't have a 'text selection' object in their scriptable dictionary, so you have to do all kinds of workarounds. See these examples:
tell application "Safari" to set selectedText to (do JavaScript "(''+getSelection())" in document 1)
tell application "System Events" to tell application process "TextEdit" to tell attribute "AXSelectedText" of text area 1 of scroll area 1 of window 1 to set selectedText to its value
tell application "Microsoft Word" to set selectedText to content of text object of selection
You can also script "System Events" to simulate the keystroke of command-c in order to copy text.
tell application "System Events" to keystroke "c" using {command down}
delay 1
set selectedText to the clipboard
If you need more specific help, post your code and indicate what app you are working with. If it is not a scriptable app, then you'll have to use the last method, calling System Events. Or, it's possible you can use an OS X Service, which you also asked about.
When you create a Service in Automator, you create a new Workflow of the type Service. Then, simply make sure that at the top of the window it says:
"Service receives selected text".
You can then use Automator actions to interact with the selected text that gets passed to the actions that follow.
Not all programs are compatible with Services, unfortunately.
To see how it works, try this very simple Automator service:
Create a Service in Automator and choose Text and Every application as input.
The first workflow step is Execute Applescript.
The Applescript's input parameter contains the selected text.
Set the Applescript to
on run {input, parameters}
display dialog (input as text)
return input
end run
After saving, you will have this action available in the context menu whenever you have selected text.
Maybe the naming is different, I don't know the English descriptions. But I hope this is a good starting point for you.
Have fun, Michael / Hamburg
I've successfully managed to get AppleScript to manipulate the background colors in my Numbers document by refering to the background color property. However, I would like to set the borders of a selection or range of cells as well.
I've looked in the Dictionary in AppleScript Editor for a command or property that could help me out here, but I haven't found anything. Is it the case that it's not possible to create borders in Numbers with AppleScript?
There is nothing in the AppleScript dictionary to allow this functionality (which is awful, IMO, but perhaps Apple will add this in the future if enough people complain).
You can use SystemEvents to interact with the interface of Numbers to add the borders:
-- Set the selection range
tell application "Numbers"
make new document
tell front document to tell active sheet to tell table 1
set selection range to range "A3:C6"
end tell
end tell
-- Asks System Events to interact with the interface
tell application "System Events" to tell application process "Numbers"
-- Asks the main window to click on the button called 'Cell' (the name depends of your system language)
tell window 1
click radio button "Cell" of radio group 1
-- Scroll area 4 is the inspector area
tell scroll area 4
-- The first incrementor of the inspector is the borders' one.
increment incrementor 1
end tell
end tell
end tell
If you want to manipulate other elements of the interface, I recommend using the Accessibility Inspector app that comes with Xcode.
What is the simplest way to get the name of the application that has a window with title "someWinTitle".
I know that it can be found by cycling through all the applications in loops but is there an elegant one liner to accomplish this?
You can use filters in AppleScript to get a list of filtered objects, if the application supports it of course. Here's an example that will return the process(es) whose browsers has this page open.
tell application "System Events"
get name of every process whose name of every window contains "applescript - Get the name of the application with window title \"someWinTitle\" - Stack Overflow"
end tell