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.
Related
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.
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.
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
How can I make applescript write the value stored in a variable wherever the insertion point happens to be, no matter what file or application happens to be in focus?
You must simulate keyboard entries via Keystroke command. But it means you must convert your variable in string first. The second issue you will have is that your application must be activate, and your script running (I guess in background), should not become upfront. Something like bellow (in this case, Application = TextEdit)
set myVariable to 10
tell application "TextEdit"
activate
delay 1
tell application "System Events" to keystroke (myVariable as string)
end tell
I am currently using applescript to setup various parameters of the desktops at my university. So far my script successfully changes the desktop background and the dock size.
The problem at hand is when I run the script, majority of the time, the icons on the desktop never change.
Here is the script I wrote to alter the desktop icon's size and grid spacing:
tell application "System Events"
set finderPrefsFile to property list file "~/Library/Preferences/com.apple.Finder.plist"
tell finderPrefsFile
tell property list item "DesktopViewSettings"
tell property list item "IconViewSettings"
set value of property list item "gridSpacing" to "100"
set value of property list item "iconSize" to "32"
set value of property list item "arrangeBy" to "none"
end tell
end tell
end tell
end tell
#Restart Finder for changes to take effect.
do shell script "killall Finder"
How should I go about altering the script to make it work all the time (I would like to eventually share this script with some of my classmates).
P.s.
Here is the full script
I didn't get your script work reliable. I played around with timeouts but didn't get the Finder to refresh using the new settings.
But I found to set some view options directly using vanilla AppleScript:
tell application "Finder"
activate
set iconViewOptions to desktop's window's icon view options
tell iconViewOptions
set arrangement to not arranged
set icon size to 32
set shows item info to false
set shows icon preview to false
end tell
quit
end tell
delay 1
tell application "Finder" to activate
The AppleScript quit-handler works more reliable then do shell script "killall Finder", maybe the killall is too hard...
The delay 1 does the magic to give the Finder the time to breath before get up again and using it the Script works each time...
But one thing is AFAIK not possible in Finder scripting: Setting the grid space :-/
Greetings, Michael / Hamburg