Applescript: How to copy paste and click button from 2 applications? - applescript

I need to copy some data from Numbers and move them over to a GUI in Google Chrome browser. I need to click a button first to open up a textbox before pasting. Right now it will only copy the data from Numbers and will throw an error System Events got an error: Can’t get application process "Google Chrome" of process "Google Chrome".
Here is my script:
tell application "Numbers" to activate
tell application "System Events"
tell process "Numbers"
keystroke "c" using command down
end tell
end tell
delay 0.2
tell application "Google Chrome" to activate
tell application "System Events"
tell process "Google Chrome"
click button "Add a New Value" of application process "Google Chrome"
keystroke "v" using command down
keystroke return
end tell
end tell

The error message says you are referencing process "Google Chrome" of process "Google Chrome" which occurs in the click button line.
tell process "Google Chrome"
click button
is the same as
click button of process "Google Chrome"
so
tell process "Google Chrome"
click button "Add a New Value" of application process "Google Chrome"
is the same as
click button "Add a New Value" of process "Google Chrome" of process "Google Chrome"
which causes the syntax error.
The solution is to change the line to
click button "Add a New Value"

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

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 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 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.

How do I do while in an AppleScript to wait for a quit action?

I'm trying to send a URL to chrome for viewing flash, quit Safari in the meanwhile so it's not using up memory, and then as soon as I quit Chrome, go back to Safari. It's not predictably going back to Safari after I quit Chrome, so I need help with the repeat loop. I want to run this as a service. Thanks!
property theURL : ""
on run {input, parameters}
tell application "Google Chrome"
activate
end tell
tell application "Safari"
activate
end tell
tell application "System Events"
keystroke "j" using {command down} -- Highlight the URL field.
keystroke "c" using {command down}
keystroke "w" using {command down}
end tell
delay 0.1
tell application "Safari"
quit
end tell
tell application "Google Chrome"
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of front window to the clipboard
activate
end tell
repeat
if application "Google Chrome" is not running then exit repeat
delay 5
end repeat
tell application "Safari"
activate
end tell
return input
end run
Update! Here's the working script:
property theURL : ""
on run
tell application "Safari"
activate
set theURL to URL of document 1
quit
end tell
tell application "Google Chrome"
activate
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of window 1 to theURL
activate tab 1
end tell
repeat
tell application "System Events"
if "Google Chrome" is not in (name of application processes) then exit repeat
end tell
delay 5
end repeat
tell application "Safari"
activate
end tell
end run
You could try this...
repeat
tell application "System Events"
if "Google Chrome" is not in (name of application processes) then exit repeat
end tell
delay 5
end repeat
Also I always avoid using keystrokes and other gui scripting stuff if I can avoid it. They're not 100% reliable. As such I suggest you transfer the url like this...
tell application "Safari" to set theURL to URL of document 1
and...
tell application "Google Chrome" to set URL of active tab of window 1 to theURL

Resources