Get the name of the application with window title "someWinTitle" - applescript

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

Related

AppleScript to remove icon from dock

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.

How to browse Applescript object "DOM" for a given object?

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.

AppleScript / Automator : use selected text as variable

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

Set focus to specific window of an application using applescript

How can I set focus to a specific window of a given application using applescript?
I have several iTerm2 windows running on different displays. I want to set focus to a specified window using applescript.
I need two things, one script that collects the window ID's and prints them to stdout. I've got this:
tell application "iTerm"
set wins to id of every window
end tell
which prints 6 integers: 3034, 2528, -1, -1, -1, -1
Bonus Question: What are the four -1's ?
Then I try:
tell application "System Events"
activate window 3034
end tell
Upon which the only thing happening is that I lose focus of my current terminal (in which I am typing these commands), not matter whether I specify 3034 or 2528 as the ID.
You almost have it. You can filter out the "-1" window IDs as by only looking at visible windows:
tell application "iTerm 2"
set wins to id of every window whose visible is true
end tell
I figured this out by looking at the results of:
tell application "iTerm 2" to properties of every window
I noticed that the "-1" windows have the property visible:false
Then you can tell the window ID directly to the iTerm application instead of system events:
tell application "iTerm 2"
activate window 13195
end tell

IF Statesment using AppleScript

I'm trying to different events depending on what application is currently the "open" application - The one which is foremost of the screen. I have managed to save the name of the application using a variable. Using this code.
tell application "System Events"
item 1 of (get name of processes whose frontmost is true)
set openWindow to (get name of processes whose frontmost is true)
do shell script "echo " & openWindow & " > /Users/name/temp/currentWindow.txt"
end tell
I then tried to use this code do different events for each open application
tell application "System Events"
if openWindow = "AppleScript Editor" then
display dialog "my variable: " & openWindow
end if
end tell
However this code does not apper to do anything, I don't have any error messages or anything however the code doesn't display the dialog box. If I place the code for the dialog box in the first section of code it will display the name of the open application.
Any ideas on how to get this to work, it would be very helpful
To explain your problem, it's because of this code...
set openWindow to (get name of processes whose frontmost is true)
That code returns a list of items. Notice you asked for processes (plural), so you can get more than one so applescript gives it to you as a list whether there's one or more items found. What's strange is that in the line above this you do ask for "item 1" of the list but for some reason you don't do anything with that line of code. Normally I would write that line of code like this so I only get item 1...
set openWindow to name of first process whose frontmost is true
Anyway, you can't compare the string "AppleScript Editor" to a list {"AppleScript Editor"}. They are not equal so your if statement is never true.
Display dialog displays a string. So when you move that code outside the if statement, applescript is smart enough to convert your list into a string so it can be displayed.
So bottom line is you are getting a list and you must access the items of the list. The items are strings so get one (in your case you want item 1) and use that in the if statement.
Hopefully you can learn from this explanation. Good luck.
In the first script cast the openWindow variable to a string:
set openWindow to (get name of processes whose frontmost is true) as string

Resources