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"'
Related
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
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.
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 ]
I'm wondering whether there is a way to write code for Mac OS 10.5 which will minimize and restore a window. What language would it be in? Could someone please give me an example or direct me to documentation on Apple's developer site I should look at?
Thanks!
Try this applescript:
tell application "Safari"
set miniaturized of window 1 to true
end tell
Apple | AppleScript
AppleScript is Apples native scripting language. The official link above includes very useful information on how you can get started.
I suspect that the following URL is what you may need. It provides information and sample code for scripting and automation in AppleScript:
AppleScript | Scripting and Automation
I had trouble with these working in 2023.
This worked for me to open chrome and then minimize.
tell application "Google Chrome"
activate
delay 1
end tell
tell application "System Events" to set visible of process "Google Chrome" to false
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