AppleScript that reads active application name, title and URL - macos

I need AppleScript that, when lunched from command line, will return three things:
current active application name; ex. "Google Chrome", "Safari",
"Steam", "iTunes", "Atom" ect.
current active application title, if there is one
if current active application is a browser, I want the current
active tab URL
examples:
Google Chrome; AppleScript: How to get URL of Safari tab which failed
to load? - Stack Overflow; https://stackoverflow.com/
iTunes; iTunes
iTerm2; 1. node test.js (sleep)
Safari, GitHub - rails/rails: Ruby on Rails; https://github.com/rails/rails
I know there are some similar questions w. answers here on stackoverflow, but I am not able to pice them together to make all of this work. Would appreciate help much.

I wanted to make a script that does exactly this running in the background so that I can better keep track of my time. Here's what I came up with:
tell application "System Events"
set frontmostProcess to first process where it is frontmost
set appName to name of frontmostProcess
end tell
tell application appName
set windowName to the name of the front window
end tell
if appName is equal to "Safari" then
tell application "Safari"
set theURL to the URL of the current tab of the front window
end tell
else if appName is equal to "Google Chrome" then
tell application "Google Chrome"
set theURL to the URL of the active tab of the front window
end tell
else
set theURL to ""
end if
return (appName, windowName, theURL)
Credit goes to https://apple.stackexchange.com/a/171738 for getting me on the right track.

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

AppleScript for getting the frontmost url from Safari and open it in Google Chrome Incognito

I am trying to write an applescript that will get the current frontmost url from Safari and opens the same url in Google Chrome Incognito.
So far I know how to get the frontmost url from Safari, I am not sure how to open that url in Chrome incognito.
MWE
tell application "Safari" to return URL of front document
tell application "Safari" to return name of front document
References:
Applescript to get the URL from Safari
The following example AppleScript code assumes Safari is opened to a page with a URL and that Google Chrome exists on the system, however, if so, will do as you've requested.
Note: There is no error handling included to ensure Safari is opened to a page with a URL or whether or not Google Chrome exists on the system. That is for you to add.
tell application "Safari" to set theURL to the URL of the front document
do shell script "open -na 'Google Chrome' --args -incognito " & theURL's quoted form
The Safari line of code can also be written as:
tell application "Safari" to set theURL to the URL of the current tab of the front window
Here is an example with some error handling that uses UI Scripting and System Events instead of a do shell script command to open the incognito window in Google Chrome:
Example AppleScript code:
if running of application "Safari" then
tell application "Safari"
if the URL of ¬
the current tab of ¬
the front window ¬
does not contain missing value then
set theURL to ¬
the URL of ¬
the current tab of ¬
the front window
else
return
end if
end tell
tell application "Google Chrome" to activate
delay 1
tell application "System Events" to ¬
keystroke "n" using ¬
{shift down, command down}
delay 1
tell application "Google Chrome" to ¬
set the URL of ¬
the active tab of ¬
the front window to theURL
end if
Update to address comment:
... is there a way if there is an incongito window already open, then open new url in a new tab?
Example AppleScript code:
if running of application "Safari" then
tell application "Safari"
if the URL of ¬
the current tab of ¬
the front window ¬
does not contain missing value then
set theURL to ¬
the URL of ¬
the current tab of ¬
the front window
else
return
end if
end tell
if running of application "Google Chrome" then
tell application "Google Chrome"
if mode of front window is "incognito" then
make new tab at end of ¬
front window with properties {URL:theURL}
end if
end tell
end if
end if
Update to address additional comments:
The following example AppleScript code contains the necessary error handling to accommodate normal possible scenarios of the state of Google Chrome in regards to opening the URL of the current tab of the front window of Safari in an incognito window/tab of Google Chrome:
Example AppleScript code:
if running of application "Safari" then
tell application "Safari"
if exists front window then
if the URL of ¬
the current tab of ¬
the front window ¬
does not contain missing value then
set theURL to ¬
the URL of ¬
the current tab of ¬
the front window
else
return
end if
else
return
end if
end tell
if running of application "Google Chrome" then
tell application "Google Chrome"
set incognitoWindowIDs to ¬
the id of every window ¬
whose mode is "incognito"
if incognitoWindowIDs is {} then
activate
my openNewIncognitoWindow()
delay 1
set URL of ¬
the active tab of ¬
the front window to theURL
else
make new tab at end of ¬
window id (first item of incognitoWindowIDs) ¬
with properties {URL:theURL}
end if
end tell
else
tell application "Google Chrome"
activate
delay 1
my openNewIncognitoWindow()
delay 1
set URL of ¬
the active tab of ¬
the front window to theURL
end tell
end if
end if
on openNewIncognitoWindow()
tell application "System Events" to ¬
keystroke "n" using ¬
{shift down, command down}
end openNewIncognitoWindow
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
If you don't mind using JavaScript, it's just few lines of code:
const chrome = Application('Google Chrome')
const safari_url = Application('Safari').documents[0].url()
chrome.Window({ mode: 'incognito' }).make()
chrome.windows[0].activeTab.url = safari_url

Applescript to open youtube in safari private mode

How can the youtube be opened in safari private mode?
I have tried this, but it is not working:
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Private Window" of menu "File" of menu bar 1
open location "https://www.youtube.com" -- this will open default browser
end tell
end tell
My default browser is Chrome, and it opens youtube in chrome not in safari private mode.
How to fix this?
The following example AppleScript code works for me:
For Safari use:
activate application "Safari"
tell application "System Events" to ¬
click menu item "New Private Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Safari"
tell application "Safari" to ¬
set URL of current tab of ¬
front window to "https://www.youtube.com"
Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.
For Google Chrome use:
activate application "Google Chrome"
tell application "System Events" to ¬
click menu item "New Incognito Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Google Chrome"
tell application "Google Chrome" to ¬
set URL of active tab of ¬
front window to "https://www.youtube.com"
Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
I don't really have a better solution for Safari than the one offered by #user3439894. The only thing I would have done differently would be to make the new private window using this code (which is probably "6 of 1 or 1/2 a dozen of the other")
tell application "Safari" to activate
tell application "System Events" to tell its application process "Safari"
set frontmost to true
keystroke "n" using {shift down, command down}
end tell
However, you may prefer this solution for Google Chrome because it does not require the use of System Events and it does not require Google Chrome to be active or frontmost.
tell application "Google Chrome"
set incognitoWindow to (make new window with properties {mode:"incognito"})
repeat while loading of active tab of incognitoWindow
delay 0.1
end repeat
set URL of active tab of incognitoWindow to "http://youtube.com"
end tell
tell application "Opera"
try
make new window with properties {mode:"incognito"}
end try
set URL of active tab of front window to "https://yoururl.com"
end tell
I wrapped make new window with properties {mode:"incognito"} in try because I get an error "Opera got an error: AppleEvent handler failed." number -10000 because i use Opera browser.
It is not necessary when using Google Chrome.

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

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