Open new chrome window with applescript alfred workflow - applescript

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

Related

Activate the previous app as soon as new app opens

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

applescript click missing value

tell application "Google Chrome" to activate
delay 0.5
tell application "System Events"
tell process "Google Chrome"
click at {1067, 79}
click at {1026, 220}
end tell
end tell
here is my applescript, I tried to open and extension by first click in chrome and then second click to download the video inside of the extension, however, only the first command was executed and the second return "missing value". I don't know why. I've also tried add a delay between the two command, it still doesn't work. Any one can help?
tell application "Google Chrome" to activate
delay 0.5
tell application "System Events"
tell process "Google Chrome"
click at {1067, 79}
delay 1
click at {1026, 220}
end tell
end tell
if this is what you mean^ then try this:
tell application "Google Chrome" to activate
delay 0.5
tell application "System Events"
tell process "Google Chrome"
click at {1067, 79}
end tell
end tell
delay 0.5
tell application "Google Chrome" to activate
tell application "System Events"
tell process "Google Chrome"
click at {1026, 220}
end tell
end tell
Turns out that if it "can't click" it will return "missing value" for a given coordinates.
This seems to happen for instance when I click into a chrome browser. If I click into Safari, it works great. If I click into a firefox, it returns and says it clicked, but doesn't actually do anything in the browser. Weird.
Workaround: Use 3rd party app MouseTools to click instead. Refs:
https://stackoverflow.com/a/12887429/32453
https://stackoverflow.com/a/22595044/32453
Though I suppose you could write your own obj-c code to do it as well, etc.

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.

Open URL and Activate Google Chrome via Applescript

I have the following AppleScript that I wrote many years ago. I use this code to program buttons on my Harmony One universal remote to access online video services via Google Chrome. The code is not working. Google Chrome doesn't launch. I am running the code via RemoteBuddy. The code complies fine, but does not work.
Anyone have any thoughts on what might be the problem, or how I can improve the script to make it work?
tell application "System Events" to set open_applications to (name of everyprocess)
if (open_applications contains "Google Chrome") is true then
tell application "Google Chrome" to quit
else
tell application "Google Chrome"
activate
open location "http://xfinitytv.comcast.net"
end tell
delay 1
tell application "Google Chrome" to activate
end if
Try it this way:
tell application "Google Chrome"
if it is running then
quit
else
activate
open location "http://xfinitytv.comcast.net"
delay 1
activate
end if
end tell
Note: it's using the newer "Enhanced Application Model" (second line), more info here: How to check in AppleScript if an app is running, without launching it - via osascript utility
tell application "Google Chrome"
open location "http://WEBSITEHERE.com"
end tell
osascript -e 'tell application "Google Chrome" to open location "http://yourlink.com.html"'

Mac: reloading document in Chrome or Firefox?

How can I tell Chrome or Firefox to reload the document in the top window? Here's what I'm using for Safari:
osascript -e '
tell application "Safari"
activate
do JavaScript "history.go(0)" in document 1
end tell
'
Here's the code for Chrome:
tell application "Google Chrome"
tell the active tab of its first window
reload
end tell
end tell
Or more concisely:
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
I do not think Firefox or Chrome have special Applescript support, but you can send the keystrokes (Cmd-R) to refresh the page:
tell application "Firefox"
activate
tell application "System Events" to keystroke "r" using command down
end tell
Here's another way to do it in Safari without using JavaScript:
tell application "Safari"
tell its first document
set its URL to (get its URL)
end tell
end tell
Automator
Open Automator and choose a New Document
Choose Service
Set Service receives to no input
Choose Run AppleScript action from the action list.
Paste the following code in the script:
 
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
Save the the service, for example, using the name Chrome Refresh
System Preferences
Open System Preferences > Keyboard
In the Shortcuts tab, choose Services
Assign a new shortcut
The following answers above work well but using them result in DevTools refreshing in a new tab if it was the last tab/window in focus. I don't want DevTools to refresh in a new tab, I just want the first tab to refresh regardless of last focus/active and this worked well for me. Leaving for someone searching for this use case as well.
tell application "Google Chrome"
activate
tell application "System Events"
tell process "Google Chrome"
keystroke "r" using {command down, shift down}
end tell
end tell
end tell

Resources