Applescript to open youtube in safari private mode - applescript

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.

Related

How to click on add extension button of chrome using apple script

I am trying to automate my chrome extension addition to chrome.
I am able to click on add extension from chrome web store using apple script but I am able to click on the Add extension button.
My current code is
tell application "System Events"
tell application process "Google Chrome"
set frontmost to true
--click button 1 of group 1 of sheet1 of window 2
keystroke "´"
tell window 1
tell (group whose title is "Add "Symantec Extension"?")
get properties
tell button 1
perform action "AXPress"
end tell
end tell
end tell
end tell
end tell
I am trying to click on this button
Testing under macOS Catalina with Google Chrome 89.0.4389.90, the following example AppleScript code worked for me, from Script Editor:
tell application "System Events" to ¬
click button "Add extension" of ¬
group 1 of ¬
sheet 1 of ¬
window 1 of ¬
process "Google Chrome"
Notes:
I only had one window with two tabs open in Google Chrome with the sheet showing when I ran that code.

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 an application in full-screen mode?

I'm trying to program Alfred to open my Terminal, Sublime Text, and Chrome with a workflow.
I would like for my terminal to open normally as a window, but I've been trying to get Chrome and Sublime to open full screen.
I was able to get Chrome to open up in full screen mode with:
on alfred_script(q)
tell application "Google Chrome"
tell window 1 to enter presentation mode
end tell
end alfred_script
However, this did not translate to work with my Sublime Text.
What am I missing here?
Another way to do this assuming you have not changed the default keyboard shortcut for "Enter Full Screen" is simply to have System Events invoke that shortcut (⌃⌘F). As with the other approach I've seen to doing this (changing the value of AXFullScreen—see mklement0's answer here for a thorough discussion of this method), this requires making the relevant window active.
For instance, to toggle the full-screen state of the frontmost window in Safari, run:
tell application "Safari" to activate
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
As found here (i need an applescript to open safari in full screen an to hide the toolbar on mavericks). The make new document line prevents the can't get window 1 error by opening a new tab if one has not previously been opened.
tell application "Safari"
make new document
activate
delay 3
tell application "System Events" to tell process "Safari"
set value of attribute "AXFullScreen" of window 1 to true
end tell
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.

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