Activate the previous app as soon as new app opens - macos

I want Applescript that trigger as soon as Mail app opens and if mail app opens then it should wait for 10 seconds and after that it should activate the previous app (like what cmd + tab does).
I have used the following code but in order to achieve that, it should run in the background all the time. I have no clue how to achieve this.
tell application "System Events"
set frontmostApplicationName to name of 1st process whose frontmost is true
end tell
tell application "Mail"
activate
end tell
tell application frontmostApplicationName
activate
end tell

Related

Open new chrome window with applescript alfred workflow

on alfred_script(q)
tell application "Google Chrome"
if it is closed then
activate
end if
else
make new window
tell application "System Events" to set frontmost of process "Google Chrome" to true
activate
end else
end tell
end alfred_script
What is wrong is my appleScript code
I just want to open new terminal if google chrome is open otherwise just run the google chrome.
It will be a alfred work flow.
You don't need to work with process of an application.
on alfred_script(q)
tell application "Google Chrome"
if its running is true then make new window
activate
end tell
end alfred_script
Note: you can omit is true as well. if its running then make new window
You don't need any of your conditions. This will launch the app and make a new window if it's not running. If it is running, it will be brought frontmost and a new window will be made.
tell application "Google Chrome"
activate
make new window
end tell
That said, you will probably end up with too many blank windows. This will check whether Google Chrome is already running. If it is, it will bring it frontmost. If there's an existing window or there's no window a new one will be created. If Chrome is not running it will be launched with a new window (its default start state).
tell application "System Events" to (name of processes) contains "Google Chrome"
set chromeRunning to result
tell application "Google Chrome"
activate
if chromeRunning then
make new window
end if
end tell

Delay in an Alfred 2, using Automator and Apple Script to open "Stickies" and create a new note

Basically my goal is to code a key command (option-s) to activate Stickies and create a new note. Right now I have an Alfred 2 generated Automation which links the hot key to the following script:
on alfred_script(q)
tell application "Stickies" to activate
delay .2
tell application "Stickies" to activate
delay .01
tell application "System Events"
keystroke "n" using command down
end tell
end alfred_script
The two activate commands are my attempt to deal with a bug where it opens the application, but doesn't bring it to front. It works seamlessly when the application is open in the background, but it's slow and creates a screen flash when the application isn't already running. The delay is not coming from the application itself because I can open the application and hit command-n as fast as possible, and it always works.
(By the way if you have an idea for how I could hide all other notes and just show the new one, that would be awesome!)
Try this:
launch application "Stickies"
tell application "System Events" to tell process "Stickies"
click menu item "New Note" of menu "File" of menu bar 1
set frontmost to true
end tell
If you run the script by pressing option-s, there might not be enough time to release option before keystroke "n" using command down.
Or this doesn't raise the windows for other notes:
launch application "Stickies"
tell application "System Events" to tell process "Stickies"
click menu item "New Note" of menu "File" of menu bar 1
end tell
do shell script "open -a Stickies"
activate app "Appname" and set frontmost of "Appname" to true raise all windows, but do shell script "open -a Appname" raises only one window.
Hotkeys also have a short delay by default in Alfred, but you can reduce it by changing the trigger behavior:
You could try this alternate way, might have a different effect.
tell application "System Events"
tell process "Stickies"
set frontmost to true
keystroke "n" using command down
keystroke "Hello World" & linefeed & "I'm a new note!"
end tell
end tell
Hiding all other notes, i'd say start a new question for that.

AppleScript: How to reload a Safari window without bring the window to the foreground while I am in other Desktop space

I want to write an AppleScript to achieve a simple task. That is, I open a Safari window on Desktop 1 and automatically reload the browser every 30 minutes while I do my daily work on Desktop 2.
Here is my script:
tell application "Safari"
activate
tell application "System Events"
tell process "Safari"
keystroke "r" using {command down}
end tell
end tell
end tell
This script works. However, whenever the script is executed, my current Desktop 2 will be brought back to Desktop 1. It is distracting to my workflow.
Is there any way to just let Safari to reload in the background without bring Safari window to foreground on Desktop 1?
I have done a couple of searches; many of them say I should not use "activate" in my script. I tried that but then the script will just do nothing.
You can simply get the URL of document 1, then tell Safari to set the URL of document 1 to that URL.
The effect is the page will reload. Without bringing Safari to the front or jumping to Safari while you are in another space.
tell application "Safari"
set docUrl to URL of document 1
set URL of document 1 to docUrl
end tell
Also a simple on idle handler and saved as an Application (Stay open checked) that will run every 1 minute but not when Safari is fronmost. i.e when you are actually using it.
on idle
set frontApp to name of application (path to frontmost application as text)
if frontApp is not equal to "Safari" then
tell application "Safari"
set docUrl to URL of document 1
set URL of document 1 to docUrl
end tell
end if
return 60
end idle

Applescript halts at a line and then won't continue (telling a not running app to hide)

So I have a very basic Applescript here that basically hides all my social networking related apps, and then a similar one that unhides them. Problem is that if one of these apps isn't running then the entire thing halts at that line and won't set the ones below that line to hidden either.
Here's the script:
tell application "System Events" to set visible of process "Twitter" to false
tell application "System Events" to set visible of process "Wedge" to false
tell application "System Events" to set visible of process "Facebook" to false
tell application "System Events" to set visible of process "Adium" to false
tell application "System Events" to set visible of process "Messages" to false
Am I doing this the wrong way? Do I have to check to see if each one of these is running first, or is there some way to get it to continue with the script either way?
EDIT: Experiencing the same thing with this script, which I use with an Alfred hotkey to quit specific apps and start the screensaver when I leave work:
tell application "Adium" to quit
tell application "Messages" to quit
tell application "1Password" to quit
tell application "iTunes" to quit
tell application "ScreenSaverEngine" to activate
So basically if iTunes - for instance - isn't running then the screensaver will not start, but the 3 apps before that line will all quit.
You can wrap each statement in a try block so it will fail silently:
set myApps to {"iTunes", "1Password"}
repeat with anApp in myApps
try
tell application anApp to quit
end try
end repeat
You Could use the try command as specified above and use on error do nothing to do it.
Hint: with the [Enhanced Application Object Model][1] you can also do things like this:
if application "iTunes" is running then
tell application "iTunes" to quit
end if
or
tell application "iTunes"
if it is running then
pause
end if
end tell
[ How to check in AppleScript if an app is running, without launching it - via osascript utility ]

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

Resources