Applescript doesn't recognize full-screen windows (?) - applescript

I need a script that involves going through windows of a specific process, but for some reason it doesn't recognize windows that are in "fullscreen-mode", so for example, the following script works (given you have at least one Finder window):
tell application "System Events"
set procList to a reference to (every process whose name is "Finder")
repeat with proc in procList
repeat with win in (windows of proc)
say "I found at least one window"
end repeat
end repeat
end tell
But if you try to toggle "fullscreen" for that window, the script no longer recognizes it. Why is that?

Related

Applescript or Shell Script to close every not-in-use terminal window in OSX

I have a an application that runs in a UI user session in OSX (a mono app that runs in a terminal window, a command line program). Sometimes this mono app exits for whatever reason, and a shell script that runs every 2 minutes as launchd checks to see if ps ax | grep "mono" only returns the grep command as the only mono process. If so, spawn a new terminal window and in that terminal window run the mono program again.
Anyway, sometimes this leaves a large number of terminal windows open. What I would like is either to append to this shell script or perhaps in Applescript which I can call in my mono app to close every terminal window not running a mono process. Can Applescript determine if a terminal window is running a certain program? Can shell determine that? If so, how?
How about this.
Change processCheckName to your process
set processCheckName to "du"
tell application "Terminal"
--activate
set theWindows to every window of it
repeat with thisWindows in theWindows
set biglist to {}
set theTabs to every tab of thisWindows
repeat with i from 1 to number of items in theTabs
set this_item to item i of theTabs
set theProc to processes of this_item
if theProc contains processCheckName then
copy id of thisWindows to end of biglist
end if
end repeat
if (id of thisWindows) is not in biglist then
close thisWindows
end if
end repeat
end tell
I looked at this post: osascript - How to close a Terminal tab using AppleScript? - Stack Overflow
tell application "Terminal"
activate
set wns to every window of it
repeat with aWn in wns
set theTabs to every tab of aWn
repeat with i from (count theTabs) to 1 by -1
set aTab to item i of theTabs
if not (get busy of aTab) then
tell aWn to set selected tab to tab i of aWn
do script "exit" in tab i of aWn
end if
end repeat
end repeat
end tell
If the process isn't busy, then it obviously isn't running something at the moment, which was my take on this.
The problem is, that if your mono process is running in the foreground, the terminal being busy, then I guess an script you run, will be put on hold. What you can do, is to extract the output (text) of the terminal window, and look for your mono process there. (I really figured, closing all windows that weren't busy was the best. Say if you have a work window, then you can assign a 'special title' for that window, and thereby, using an if test to excempt it from being closed. (It is probably not busy most of the time.)

using applescript in textwrangler to send code to julia

I am trying to setup a TextWrangler script that automatically sends selected code to Julia. Copying a bit from a similar script that does the job for sending code to R I tried the script
tell application "TextWrangler"
set the_selection to (selection of front window as string)
if (the_selection) is "" then
set the_selection to line (get startLine of selection) of front window as string
end if
end tell
tell application "Julia-0.2.1"
activate
cmd the_selection
end tell
It does not work. Probably because of the line containing "cmd the_selection". Anyone a suggestion on how to fix this?
Well if Julia isn't scriptable as #khagler says, then you can usually use gui scripting. This will work as long as Julia has an edit menu and the keyboard shortcut for paste is cmd-v (like in most applications) and your cursor is placed in the proper location for the paste to work. Good luck.
tell application "TextWrangler"
set the_selection to (selection of front window as string)
if (the_selection) is "" then
set the_selection to line (get startLine of selection) of front window as string
end if
end tell
set the clipboard to the_selection
tell application "Julia-0.2.1" to activate
delay 0.2
tell application "System Events" to keystroke "v" using command down
EDIT: I see in your comments that a new Julia window opens every time the script is run. That might be happening because of the line... tell application "Julia" to activate. You might try this line of code in its place. It's another method to bring Julia frontmost before issuing the keystroke commands.
tell application "System Events" to tell process "Julia-0.2.1" to set frontmost to true
Note that if this doesn't bring the Julia window frontmost then it's because the process name is wrong. Sometimes the process name is different than the application name. If true you'll have to figure out the name of the Julia process which you can do by running this and finding its name. You may have to use "Terminal" if Julia runs in a Terminal window.
tell application "System Events" to return name of processes
Julia is not scriptable, so you won't be able to send your code this way. You could try this:
do shell script "julia -e '" & the_selection & "'"
This is equivalent to typing a command into a Terminal window. Depending on what exactly is in your selection, you might need to save it as a file first and then pass the file path instead. Also, note that if julia is not in your path (as would be the case if you just copied the Julia app to your Applications folder) you'll need to specify the full path to the actual executable within the .app package: /Applications/Julia-0.2.1.app/Contents/Resources/julia/bin/julia.

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

Listing all windows of all applications

I am trying to write an applescript script that resizes all open windows. In order to make sure that I'm getting to all the windows, I'm making my script say the name of the application as well as the number of open windows of that application.
Interestingly, while I hear the names of all my open applications, my script says that they all have 0 windows open. How can I fix this issue?
Here's my code:
tell application "System Events"
repeat with theProcess in (every process)
if background only of theProcess is false then
if name of theProcess is not "Finder" then
if name of theProcess is "Google Chrome" then
say "Chrome woo hoo"
say (count windows as string)
else
say name of theProcess as string
say (count windows as string)
tell theProcess
repeat with theWindow in windows
say "found a window of"
say (name of theProcess) as string
tell theWindow
click button 2
end tell
end repeat
end tell
end if
end if
end if
end repeat
end tell
I'm on Mac OS X 10.7.5, using automator 2.2.4 to write/run this applescript
You have to tell the process to count windows. After all it's the process that knows about its windows, not system events.
You have told the process to say its name e.g. "say name of theProcess as string" however you only use "say (count windows as string)"... no process is tied to that. Try "count windows of theProcess". Basically you have lines where sometimes you tell the process, other times you don't, and other times where you tell the process even though you've already told the process, so you do it twice. That's where you have "say (name of theProcess) as string" but that code is inside a "tell theProcess" block so it's already being told to theProcess.
Really you need to go through your code and be more precise. A tip... if you want to click a button in a window then the window must be frontmost on the screen otherwise you can't click it. Another tip... "name" is already a string so you don't need to coerce that to a string.
By the way, I agree with Michael Dautermann's comment to your post... there will be processes where you won't get access. But you'll find that out as you progress.
Here's how I would write your code. Basically I would get all of the variables at the beginning using a "tell theProcess" block. Then I can do stuff with those variables. I hope that helps. Notice that I only made the process frontmost which means if it has multiple windows open it will only click a button on the front window. You'll have to add code to make each window come to the front before you can click its button. Good luck.
tell application "System Events"
repeat with theProcess in processes
if not background only of theProcess then
tell theProcess
set processName to name
set theWindows to windows
end tell
set windowsCount to count of theWindows
if processName is "Google Chrome" then
say "Chrome woo hoo"
say windowsCount as text
else if processName is not "Finder" then
say processName
say windowsCount as text
if windowsCount is greater than 0 then
repeat with theWindow in theWindows
say "found a window of " & processName
tell theProcess
set frontmost to true
tell theWindow
click button 2
end tell
end tell
end repeat
end if
end if
end if
end repeat
end tell
I create a list of all open windows of visible applications on Mavericks like this:
tell application "System Events"
set this_info to {}
repeat with theProcess in (application processes where visible is true)
set this_info to this_info & (value of (first attribute whose name is "AXWindows") of theProcess)
end repeat
this_info -- display list in results window of AppleScript Editor
end tell
You need to allow any app using this to access the interface under Accessibility.

Applescript - Get status of windows ( Visible or in the dock )

I need some help for my appleScript.
For all open windows, I want to know which one is hidden (in the dock), which one is visible and which one is focused?
To list windows I use :
tell application "System Events"
set procs to processes
set windowName to {}
repeat with proc in procs
try
if exists (window 1 of proc) then
repeat with w in windows of proc
copy w's name to the end of windowName
end repeat
end if
end try -- ignore errors
end repeat
end tell
return windowName
I tried focused property:
copy w's focused to the end of windowName
and selected property:
copy w's selected to the end of windowName
But this doesn't work!
Thanks for help!
On Mac OS X 10.6 (AppleScript 2.1.2) the description property of a window of an application process (in System Events' terms) is "dialog" if the window is miniaturized (in the Dock), and some other value (such as "standard window", but can be something different depending on the application) if it's not miniaturized.
When an application is hidden altogether (using cmd+H or the "Hide" command from the application's menu), all of its windows will be hidden, regardless of whether they were miniaturized or not, so to find out whether it's hidden, use
visible of application process "<ProcessName>"
It's false when the application is hidden. To un-hide it, set that property to true.
To find out which window of an application is currently focused (frontmost/active), use
window 1 of application process "<ProcessName>"
The list of windows of an application (returned by windows of application process...) is ordered by the vertical stack: the frontmost window is first, the one behind it is second, and so on.
Since in OS X only one application is frontmost at a time, and only one window is in the foreground, you'll get at the currently focused window like this:
window 1 of (first application process whose frontmost is true)

Resources