How to get the same window between process "Safari" and application "Safari"? - applescript

I know that I can get id of windows of Safari by:
tell application "Safari" to set winId to id of first window
But I can not get id of window like:
tell application "System Events" to tell process "Safari"
set winId to id of first window
end tell
Is there any property of window can be used to identity the same window between tell process "Safari" and tell application "Safari"?

Related

Script Editor hiding when telling a different application to close/hide

Why does the following Applescript hide Script Editor?
tell application "Last.fm" to launch
tell application "System Events" to tell process "Last.fm" to keystroke "h" using command down
This code I found will hide "Last.Fm", but also hides Script Editor. Ideally, I want to replace the keystroke "h" with a keystroke "w" but then I get an error:
The document can’t be closed while the script is running.
Why does the script I wrote effect Script Editor?
I don't have Last.fm, so I tried this:
tell application "TextEdit" to launch
delay 2
tell application "System Events" to tell process "TextEdit" to keystroke "h" using command down
And sure enough, it's true, TextEdit was hidden but so was Script Editor.
Then I tried this:
tell application "TextEdit" to launch
tell application "TextEdit" to activate
delay 2
tell application "System Events" to tell process "TextEdit" to keystroke "h" using command down
TextEdit was hidden, but Script Editor was not. So I would guess that this will help in your code too. Having the target app frontmost appears to be crucial (which makes a certain amount of sense, after all).
What is the functionality you're trying to achieve here? Is this the whole script? You want last.fm to launch, and then, ideally, to close the window?
tell application "System Events" to tell process
Doesn't work because it is highly misleading - it will not actually direct a keystroke to a particular application, the keystroke goes wherever it would go when it's hit regardless of the "tell process" statement, which is annoying.
I don't use last.fm, but either of these works for me:
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
keystroke "w" using command down
end tell
or
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
click menu item "Close" of menu 1 of menu bar item "File" of menu bar 1
end tell
Note that if you plan to run the script from a key command or the script menu, and don't want it to steal focus to last.fm, you can have it launch, close the window (or hide it), and then return you to the front app when the script was run:
set appPath to the path to the frontmost application
tell application "Finder"
set appName to the name of file appPath
set appName to text 1 thru ((offset of "." in appName) - 1) of appName
end tell
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
keystroke "w" using command down
end tell
tell application appName to activate

Activate the frontmost Safari and run Javascript

I'm trying to run Javascript on the Safari, whose frontmost is true.
tell (application "Safari" whose frontmost is true) to do JavaScript theScript in current tab of first window
But I get an error like this:
execution error: Can’t get application "System Events" whose frontmost of it = true. Access not allowed. (-1723)
where I'm making mistake?
AppleScript often requires the syntax be formatted in a specific way. In the case of your script it can't differentiate identifiers because you've placed them incorrectly on the same line.
set theScript to "alert('Hello, World!');"
tell application "Safari"
set frontmost to true
activate
do JavaScript theScript in current tab of first window
end tell
You can test your script in AppleScript editor by compiling and running it. The events, replies and results should give you some indication of where problems might be.
EDIT: to deal with a process which is the frontmost (or most recent):*
set theApp to "Safari"
set theScript to "alert('Hello, World!');"
on isOpen(appName)
tell application "System Events" to (name of processes) contains appName
end isOpen
set isActive to isOpen(theApp)
if isActive then
tell application "System Events" to tell (process "Safari" whose frontmost is true)
tell application "Safari"
activate
tell application "System Events" to set frontSafari to item 1 of (get name of processes whose frontmost is true)
if (count windows) is 0 then
display dialog "There are no " & theApp & " windows open." buttons {"OK"}
else if frontSafari is "Safari" then
tell application "Safari" to do JavaScript theScript in current tab of first window
end if
end tell
end tell
else
display dialog theApp & " is not currently running." buttons {"OK"}
end if

keystroke "1" using command down - beeps instead of working

I'm writing a simple applescript that should focus an app and click "cmd+1".
This is what I wrote:
tell application "System Events"
tell application process "appname"
--Lobby focus
activate
keystroke "1" using command down
end tell
end tell
But instead of working, there's one beep and the application doesn't even take focus.
How can I solve this?
You can't tell processes to activate. Change it to set frontmost to true:
tell application "System Events"
set frontmost of process "Finder" to true
keystroke "1" using command down
end tell
Or tell the application to activate:
activate application "Finder"
tell application "System Events"
keystroke "1" using command down
end tell
If the application has no open windows, reopen opens a new default window:
tell application "Finder"
reopen
activate
end tell
tell application "System Events"
keystroke "1" using command down
end tell

Unnamed Window in Applescript

I'm scripting iTunes with applescript using UI scripting. Depending on what I'm doing an iTunes notification will appear, at which point I need to handle it. The name of the window is AXWindow: "", and I can't seam to get applescript to handle it. I've tried using the literal "", I've tried defining a variable to "", I've tried both cases with escape characters, and I've tried getting the name of the frontmost process.
tell application "System Events"
set processName to name of front window
end tell
tell button "whatever" of window processName
click
end tell
But that comes up with "error "System Events got an error: Can't get window 1. Invalid Index."" Any help on this would be greatly appreciated.
You can do something like this:
tell application "iTunes" to activate
tell application "System Events"
tell process "iTunes"
set xxx to first UI element whose role description is "dialog"
end tell
end tell
Or to find them all:
tell application "iTunes" to activate
tell application "System Events"
tell process "iTunes"
set xxx to every UI element
end tell
end tell
Well, usually notifications or user dialogs will show up as the frontmost window and stay on top of the other windows of the same application until the user (or the script) clicks something.
Thus, the dialog window (if there is any) should be accessible via the specifier window 1. You can then further check if that really is the window you're interested in by reading its properties:
tell application "System Events" to tell application process "iTunes"
properties of window 1
end tell

How do I do while in an AppleScript to wait for a quit action?

I'm trying to send a URL to chrome for viewing flash, quit Safari in the meanwhile so it's not using up memory, and then as soon as I quit Chrome, go back to Safari. It's not predictably going back to Safari after I quit Chrome, so I need help with the repeat loop. I want to run this as a service. Thanks!
property theURL : ""
on run {input, parameters}
tell application "Google Chrome"
activate
end tell
tell application "Safari"
activate
end tell
tell application "System Events"
keystroke "j" using {command down} -- Highlight the URL field.
keystroke "c" using {command down}
keystroke "w" using {command down}
end tell
delay 0.1
tell application "Safari"
quit
end tell
tell application "Google Chrome"
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of front window to the clipboard
activate
end tell
repeat
if application "Google Chrome" is not running then exit repeat
delay 5
end repeat
tell application "Safari"
activate
end tell
return input
end run
Update! Here's the working script:
property theURL : ""
on run
tell application "Safari"
activate
set theURL to URL of document 1
quit
end tell
tell application "Google Chrome"
activate
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of window 1 to theURL
activate tab 1
end tell
repeat
tell application "System Events"
if "Google Chrome" is not in (name of application processes) then exit repeat
end tell
delay 5
end repeat
tell application "Safari"
activate
end tell
end run
You could try this...
repeat
tell application "System Events"
if "Google Chrome" is not in (name of application processes) then exit repeat
end tell
delay 5
end repeat
Also I always avoid using keystrokes and other gui scripting stuff if I can avoid it. They're not 100% reliable. As such I suggest you transfer the url like this...
tell application "Safari" to set theURL to URL of document 1
and...
tell application "Google Chrome" to set URL of active tab of window 1 to theURL

Resources