Error number -128 Applescript - applescript

I have a simple applescript that opens Safari, opens a web page 100 times (long story) and then waits 300 seconds to close Safari.
Here is the code:
set theURL to "https://sites.google.com"
tell application "Safari"
activate
repeat 100 times
try
tell window 1 to set current tab to make new tab --with properties {URL:theURL}
set URL of document 1 to theURL
on error
open location theURL
end try
end repeat
end tell
delay 300
tell application "Safari"
quit
end tell
The first part of the script runs famously. When I get past the delay, I am getting an error:
error "Safari got an error: User canceled." number -128
I tried to just run it without opening multiple web pages Just activate Safari, wait 10 second and quit. I get the same problem.
Has anyone dealt with this?

I'm not getting the error you describe. However I am seeing a bug where Safari is not activating if the application was not already running when I run the code.
This code seems to work as you'd expect...
set theURL to "https://sites.google.com"
tell application "Safari" to activate
tell application "Safari"
activate
repeat 2 times
try
tell window 1 to set current tab to make new tab
set URL of document 1 to theURL
on error
open location theURL
end try
end repeat
end tell
delay 5
tell application "Safari"
quit
end tell

Related

How to open a URL with AppleScript only if no other URL checked is the right one?

I'm looking for solution but my searches was not good so far
This is my code
tell application "Google Chrome"
if it is running then
repeat with t in tabs of windows
tell t
if URL starts with "https://keep.google.com/" then
delay 1
activate
else
open location "https://keep.google.com/"
delay 1
activate
end if
end tell
end repeat
else
activate
open location "https://keep.google.com/"
delay 1
activate
end if
end tell
tell application "Google Chrome" to activate
return ""
I'm able to check the URL, but for each URL that is not Google Keep I open a new URL. So if I have Gmail, Youtube and Keep opened, I open more two keeps URLs. How to open only if no one is the right URL?
First of all, tell application "Google Chrome" in the following code snippet at the start of your code block will launch Google Chrome if it's not running.
tell application "Google Chrome"
if it is running then
So, technically that's not the proper way to use the running property in the next line because as you presently have it coded, Google Chrome is always running when if it is running then is processed.
Here is an example of how I'd rewrite your code to make proper use of the running property and only open the target URL is it not already open.
Example AppleScript code:
set theURLsList to {}
set theURL to "https://keep.google.com/"
if running of application "Google Chrome" then
tell application "Google Chrome"
repeat with t in tabs of windows
copy URL of t to end of theURLsList
end repeat
if theURLsList does not contain theURL then
open location theURL
activate
end if
end tell
else
tell application "Google Chrome"
open location theURL
activate
end tell
end if
Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

error when clicking a button on a page, "Expected end of line but found identifier."

I'm trying to get AppleScript to click on a button on a page, but it's not working. I keep getting the error "Expected end of line but found identifier" just after the 'do'.
Here's the code:
tell application "Firefox"
activate
open location "https://habitrpg.com/static/front"
end tell
delay 3
tell application "Firefox"
do JavaScript "document.getElementsById('frontpage-play-button').click();" in current tab of first window
end tell
Where am I going wrong please?
Thanks in advance!
Firefox has limited applescript support so those commands won't compile.
Also that javascript was slightly off, should have used getElementsById
You could try Safari instead.
e.g,
tell application "Safari"
activate
open location "https://habitrpg.com/static/front"
end tell
delay 5
tell application "Safari"
activate
do JavaScript "document.getElementById('frontpage-play-button').click();" in current tab of front window
end tell

Auto click a dialog button using AppleScript

I want to use AppleScript to auto login for a web by Safari.
When I open a URL and give username and password, if it is the first time or hasn't saved the password yet, it will show message: "Would you like to save this password?".
I want to use AppleScript to choose "Not now". How can I do that?
Here is my current code:
tell application "Safari"
open location "http://www.amazon.com"
activate
end tell
if page_loaded(60) then
say "Page Loaded"
tell application "Safari"
set loginURL to do JavaScript "document.getElementById('nav-your-account').getAttribute('href')" in document 1
open location loginURL
end tell
else
say "Page Failed to load or Safari didn't open"
end if
if page_loaded(60) then
say "Page Loaded"
tell application "Safari"
do JavaScript "document.getElementById('ap_email').value = 'mritjp'; document.getElementById('ap_password').value = 'mritjp'; document.forms['signIn'].submit();" in document 1
---HOW TO CONTROL TO CHOOSE "Not Now" in message "Would you like to save this password?" of safari
activate
delay 2
end tell
else
say "Page Failed to load or Safari didn't open"
end if
on page_loaded(timeout_value) -- in seconds
delay 1
repeat with i from 1 to timeout_value
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
delay 1
end repeat
if i is timeout_value then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
return true
end page_loaded
Fixed by comment of Lauri Ranta:
If access for Assistive Devices has been enabled from System Preferences, you can use System Events:
on enabledGUIScripting()
do shell script "sudo touch /private/var/db/.AccessibilityAPIEnabled" password "1" with administrator privileges
end enabledGUIScripting()
Then:
tell application "System Events" to tell process "Safari"
click button 3 of sheet 1 of window 1
end tell
If access for assistive devices has been enabled from System Preferences, you can use System Events:
tell application "Safari"
do JavaScript "document.getElementById('login_button_id').click()" in document 1
end tell
delay 1
tell application "System Events" to tell process "Safari"
click button 3 of sheet 1 of window 1
end tell

AppleScript - Help with Safari

I've been appointed the task of creating an applicaton that must
Display a dialog saying "The webpage has finished loading." when a webpage finishes loading
Determine how many items are currently in the process of downloading
For number 1, I've tried doing if the URL of the front document is "http://applescript.weebly.com" then, but the then part always runs even if the webpage doesn't load!
For number 2, I've tried this...
tell application "Safari" to get every item of window "Downloads"
But this returns references to every item, even the ones that havae already been downloaded!
Conclusion: I need help. :S
Display a dialog saying "The webpage has finished loading." when a webpage finishes loading.
There is no way to do this with just pure AppleScript. However, you can use a combination of AppleScript and javascript...
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
end repeat
display dialog "The webpage has finished loading."
end tell
WARNING: If the webpage doesn't load for some reason, the script will be stuck forever in an infinite repeat loop.
Determine how many items are currently in the process of downloading.
When you download files, they are temporarily given the name extension download, so AppleScript can tell the Finder to get the files with that extension and create an alert/dialog:
set the download_count to 0
set the download_folder to (path to downloads folder) as alias --or wherever the items are being downloaded
tell application "Finder" to set the download_count to the count of (every item of the download_folder whose name extension is "download")
if the download_count is 0 then
display dialog "There are currently no downloads in progress."
else if the download_count is 1 then
display dialog "There is currently one download in progress."
else
display dialog "There are currently " & the download_count & " downloads in progress."
end if
P.S. Thanks for honoring my web site!
I wrote the script below in AppleScript Editor to open my browser, display my ISP in the URL, go through the login process and connect to the internet. Is working perfectly. I want to add or modify it to ping "www.google.com" every hour or so, and if my connection has dropped to repeat the process below again, which will connect my computer back to the internet.
I'm just learning this script writing thing and don't have a very good undersatnding of how it works yet, so if anyone can help please expalin it in simple terms for me. Very much appreciated.
tell application "Safari"
activate
tell application "System Events"
open location "https://basrah.uswicom.com/login.php"
delay 1
keystroke return
delay 3
keystroke tab
keystroke (ASCII character 29)
delay 1
delay 1
keystroke tab
delay 1
keystroke "123456789"
delay 1
keystroke return
delay 1
keystroke return
end tell
end tell

Open URL in new Safari tab with AppleScript

Is it possible to use AppleScript to open a link in a new tab in Safari?
This will work:
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell
I think this also does what you asked for, but it is much shorter and is less browser-specific:
do shell script "open http://www.webpagehere.com"
This will open the specified URL in your default browser. And if you explicitly want to open it in Safari, use this:
do shell script "open -a Safari 'http://www.webpagehere.com'"
This should usually create a new tab and focus it (or focus an existing tab if the URL is already open):
tell application "Safari"
open location "http://stackoverflow.com"
activate
end tell
It opens a new window if "Open pages in tabs instead of windows" is set to never though.
tell application "System Events" to open location doesn't work with some URLs that contain non-ASCII characters:
set u to "http://ja.wikipedia.org/wiki/漢字"
tell application "System Events" to open location u
--tell application "Safari" to open location u
--do shell script "open " & quoted form of u
This opens a new tab even when new pages are set to open in windows:
tell application "Safari"
activate
reopen
tell (window 1 where (its document is not missing value))
if name of its document is not "Untitled" then set current tab to (make new tab)
set index to 1
end tell
set URL of document 1 to "http://stackoverflow.com"
end tell
tell application "System Events" to tell process "Safari"
perform action "AXRaise" of window 1
end tell
set index to 1 doesn't raise the window, but it makes the window appear as window 1 to System Events, which can AXRaise it.
I've been using the following script to open hundreds of docs into tabs in a single window.
tell application "Safari"
tell window 1
make new tab with properties {URL:"http://foo.com/bar"}
make new tab with properties {URL:"http://foo.com/baz"}
end tell
end tell
But that no longer works in Safari 5.1 on Lion. It would open the new tab, but it wouldn't load the URL provided in the properties glob. I modified it to the following, which now works:
tell application "Safari"
tell window 1
set URL of (make new tab) to "http://foo.com/bar"
set make new tab to "http://foo.com/baz"
end tell
end tell
Code:
tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
tell application "Safari"
set URL of document 1 to "http://www.stackoverflow.com/"
end tell
One problem is that this only works if the system's language is set to English.
It's been a while since a new answer's been posted here. I think this is the optimal way to do this. It will open Safari if it's not open, create a new window if there are no windows open, and add the tab to the current (or newly created) window.
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:theURL}
on error
open location theURL
end try
end tell
I can't comment :-/ so I will answer to say that Tim's answer (above) works as of OS X 10.8.5. This one-line version of his script also works:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
Arrgh -- the one line is overflowing. Here it is without the code tags:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
I ended up using automator to do this which was much easier and it works.
You can try following approach:
//make Safari window active and topmost
tell application "Safari" to activate
//start communication with Safari
tell application "Safari"
tell window 1
//create new tab and open specified URL
tab with properties {URL:"https://url.com"})
//make tab active
set visible to true
end tell
end tell
Also u can combine usage of Apple script within FastScript (free for 10 shortcut)
To add your script - just save script in /Library/Scripts. After you will be able to set some shortcut for new script.
If you want to open new Window than new tab u can play within next:
tell application "System Events"
tell process "Safari"
click menu item "New window" of menu "File" of menu bar 1
end tell
end tell
Note: you need to allow AppleScript to use specialCapabilities in security settings in this case.
Not the shortest solution but also works, and not only in English ...
tell application "Safari"
activate
end tell
tell application "System Events"
set frontmost of process "Safari" to true
keystroke "t" using {command down}
end tell
set myURL to "anyurl.html"
delay 2
tell application "Safari" to set the URL of the front document to myURL
Worked for me in Safari v.11
tell application "Safari"
tell window 1
make new tab with properties {URL:"https://twitter.com"}
end tell
end tell
I found a way to open a new tab in the background with Safari.
tell application "Safari"
set the URL of (make new tab in window 1) to "your.url.net"
end tell
During the time I wrote this answer I made this
tell application "Safari"
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
end try
end tell
New version
tell application "Safari"
repeat
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
if theURL is "" then exit repeat
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"
if button returned of result is "No" then exit repeat
end try
end repeat
end tell
Any suggestions will be appreciate
Best regards

Resources