Applescript to "jump" to designated application and back to previous one? - macos

I like to use this script to bring up useful applications that need to be accessed often. I would like this script to return to the previous application when run instead of just hiding it when visible. What code would nee to be changed so that when the “Mail” is visible, it will jump to the previously open application?
set appName to "Mail"
set appID to bundle identifier of (info for (path to application appName))
tell application "System Events"
if not (exists process appName) then
tell application appID to activate
else
if frontmost of process appName then
set visible of process appName to false
else
set frontmost of process appName to true
end if
end if
end tell

I don't know any way to get the second frontmost application apart from hacks like this:
tell application "System Events"
set p to process 1 where frontmost is true
set visible of p to false
delay 0.01
set a to process 1 where frontmost is true
delay 0.01
set frontmost of p to true
a
end tell
tell application "System Events"
keystroke tab using command down
delay 0.01
set a to path to frontmost application as text
delay 0.01
keystroke tab using command down
a
end tell
You might hide and then show the application though:
if (path to frontmost application) is (path to application "Mail") then
tell application "System Events"
set visible of process "Mail" to false
delay 0.01
set visible of process "Mail" to true
end tell
else
activate application "Mail"
end if

Related

AppleScript get title of a window

I try to get the title of an app (and copy it as a var)
Here is my code so far but this is failing
tell application "app"
activate
end tell
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
Result :
error "app got an error: every window doesn’t understand the “count” message." number -1708 from every window
The error message suggests that the frontmost application is not scriptable.
The only way to target a non-scriptable application's windows is via the properties of [application] process objects from the System Events context - not via its application object (only scriptable application objects have windows elements):
tell application "app"
activate
end tell
tell application "System Events"
# Get the frontmost app's *process* object.
set frontAppProcess to first application process whose frontmost is true
end tell
# Tell the *process* to count its windows and return its front window's name.
tell frontAppProcess
if count of windows > 0 then
set window_name to name of front window
end if
end tell

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

What is the code used to switch back to a previously running window via Apple's Automator?

I created a code that toggles the keyboard viewer in Automator.
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
activate application "KeyboardViewer"
end if
return input
end run
However, the Keyboard Viewer becomes the current running window and I cannot instantaneously start typing (I have to switch back to the previous window). Is there a specific code that I can add so that the previous window is highlighted again ?
You could use launch instead of activate:
tell application "KeyboardViewer"
if running then
quit
else
launch
end if
end tell
If the application wasn't open, launch usually opens a new window above other applications but below the frontmost application. Otherwise it just keeps the application on the background. You can use AXRaise to raise windows in the second case, but it also makes them look like active windows.
launch application "Terminal"
tell application "System Events" to tell process "Terminal"
perform action "AXRaise" of windows
end tell
You could also save the previous application in a variable:
set a to path to frontmost application as text
activate application "Terminal"
activate application a
If you're moving focus to a background application, you can just activate the frontmost application later:
try
tell application "SystemUIServer"
display dialog "" default answer ""
end tell
end try
activate application (path to frontmost application as text)
Try this just after the activate application "KeyboardViewer" line...
tell application "System Events" to keystroke tab using command down
EDIT: Since the original post above didn't do it for you then try this. It uses a subroutine I use to get the frontmost running application in cases just like this. Just put this code into the applescript section of your automator action...
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
set frontAppPath to my getFrontAppPath()
activate application "KeyboardViewer"
delay 0.2
tell application frontAppPath to activate
end if
return input
end run
on getFrontAppPath()
set frontAppPath to (path to frontmost application) as text
set myPath to (path to me) as text
if frontAppPath is myPath then
try
tell application "Finder" to set bundleID to id of file myPath
tell application "System Events" to set visible of (first process whose bundle identifier is bundleID) to false
-- we need to delay because it takes time for the process to hide
-- I noticed this when running the code as an application from the applescript menu bar item
set inTime to current date
repeat
set frontAppPath to (path to frontmost application) as text
if frontAppPath is not myPath then exit repeat
if (current date) - inTime is greater than 1 then exit repeat
end repeat
end try
end if
return frontAppPath
end getFrontAppPath

How to change space with applescript

I have the following applescript to skip songs in spotify. If I call the script when a fullscreen application is the frontmost, the application will not be visible after the script has finished. It will be in a different space. Is there a way that I can make the frontmost application visible again with applescript?
set front_app to (path to frontmost application as Unicode text)
tell application "Spotify"
next track
end tell
tell application front_app to activate
This probably won't get you anywhere, but you could use this script to loop through all your spaces until you see the application you're targeting...
set front_application to current application
tell application "Spotify" to next track
tell application "Finder" to set collapsed of front_application to false --makes sure the app isn't minimized
repeat
tell application "System Events" to keystroke (ASCII character 29) using {control down}
display dialog "Correct space?" buttons{"OK"} default button 1 giving up after 4 --don't click 'OK' if the current space isn't showing the target application
if gave up of the result is false then exit repeat
end repeat
set front_app to current application
tell application "Spotify"
next track
end tell
tell front_app to activate

Resources