Why does Script editor not like this edit? - firefox

I got his script:
set vURL to URL of current tab of window 1
end tell
tell application "Google Chrome"
if windows ≠ {} then
make new tab at the end of window 1 with properties {URL:vURL}
else
make new window
set URL of (active tab of window 1) to vURL
end if
activate
end tell
I'd really like to be able to just quickly open the tab I'm using in Safari in Firefox beause...
I'm told have to add more text here so, Skip this paragraph. ... a site I use a lot has some great features in Firefox but it can be slow to load so I like to switch backwards and forwards between the two.
Why won't it work if I change "Google Chrome" to "Firefox":
tell application "Safari"
set vURL to URL of current tab of window 1
end tell
tell application "Firefox"
if windows ≠ {} then
make new tab at the end of window 1 with properties {URL:vURL}
else
make new window
set URL of (active tab of window 1) to vURL
end if
activate
end tell
Thanks

You can't mix and match scripting terms - the scripting terminology (if any) for a given application is entirely up to the developer and is unique to that application. There also isn't a standard or common practice for term names, so any similarity between scripting terms of different applications would be purely coincidental and wouldn't necessarily provide the same functionality.
Looking at the scripting dictionaries for Google Chrome vs Safari and Firefox:
Firefox doesn't really have a scripting dictionary, only a default suite is available. Specifically, a window does not have either
tab, active tab, or URL elements (URL here would also be
assumed to be from StandardAdditions)
Safari windows do not have an active tab property.
You will need to target each application with its own tell statement, using the terminology specific to that application. Also note that trying to do something like using a variable to hold the application name will not work, since the scripting terminology for each application is looked up when the script is compiled.
(Edit from comments)
To open the URL of the current Safari tab with Firefox (version 69.0.1 in Mojave), you can do something like:
tell application "Safari" to set theURL to (get URL of current tab of front window)
tell application "Firefox"
activate
open location theURL
end tell

Related

How to open a tab 2 on Google Chrome or Microsoft Edge if it doesn't exist?

I am new to AppleScript and tried an AppleScript solution to open up a tab and load a webpage:
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
But if tab 2 doesn't exist, it won't do anything.
I then tried to solve it by checking whether an URL exist in any tab:
tell application "Google Chrome"
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t starts with "https://mail.google" then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
open location "https://mail.google.com/mail/u/0/#inbox"
end tell
But it is sort of an overkill, and it opens up a new tab, rather than opening up a tab 2.
I then tried
tell application "Google Chrome" -- or "Microsoft Edge"
tell window 1
open tab 2 -- added this line
tell tab 2
set URL to "https://google.com/"
end tell
end tell
end tell
but no matter it is open tab 2 or activate tab 2 or start tab 2, it wouldn't work. How can we make it work and, in general, how to find out the vocabularies or verbs that we can use?
open tab 2 is the problem. If a browser window exists and you want to create a new tab and set its URL, then:
tell application "Google Chrome" to ¬
if exists front window then ¬
make new tab at end of ¬
tabs of front window ¬
with properties ¬
{URL:"https://www.example.com"}
If a browser window exists and you want to change the URL of tab 2, then:
tell application "Google Chrome" to ¬
if exists front window then ¬
tell front window to ¬
if (exists tab 2) then ¬
tell its tab 2 to ¬
set its URL to ¬
"https://www.example.com"
Notes:
The example AppleScript code works with Microsoft Edge too.
These examples can be expanded into normal block format using if statements to determine if Google Chrome and or Microsoft Edge exists, or are already running, and if running whether or not a window exists, etc. You just need to code it according to your needs/wants.
Have a look at AppleScript Language Guide
In Script Editor have a look at the Library for the AppleScript dictionary of any application you want to write code for. From the Window menu... Library     ⇧⌘L
The example AppleScript code below shows an example of coding for the existence of either Google Chrome or Microsoft Edge and whether or not they are running when you can then write code based on the conditions. As written it favors Google Chrome but if it does exist then Microsoft Edge runs its code. To favor Microsoft Edge change the primary if statement structure to accommodate.
tell application "System Events"
set existsGoogleChrome to exists file "/Applications/Google Chrome.app"
set existsMicrosoftEdge to exists file "/Applications/Microsoft Edge.app"
end tell
if existsGoogleChrome then
if running of application "Google Chrome" then
tell application "Google Chrome"
# Do Something
end tell
else
tell application "Google Chrome"
# Do Something Else
end tell
end if
else
if existsMicrosoftEdge then
if running of application "Microsoft Edge" then
tell application "Microsoft Edge"
# Do Something
end tell
else
tell application "Microsoft Edge"
# Do Something Else
end tell
end if
end if
end if

How do I tell if the frontmost window is a NSOpenPanel / file open dialog in MacOS using Applescript?

I'm trying to automatically change the directory of the frontmost "file open dialog" or NSOpenPanel dialog with AppleScript, whether that window is part of any application. The idea is that I hit a hotkey, and it will control that dialog to switch to a particular folder.
I can't seem to find out how to find the attributes of a window that would filter it for a "file open dialog". Using the Accessibility Inspector I can find that the "class" is NSOpenPanel. How can I get the class of a window using Applescript?
If you run the following AppleScript, you can see the properties of the foremost window:
tell application "anApp" to activate
delay 1
tell application "System Events"
tell process "anApp"
properties of window 1
end tell
end tell
The app has to be active to see the properties of the windows; You will not get consistent results if the app is in the background.
The NOOpenPanel ought to be recognizable by testing for some combination of the following properties:
role description:"dialog"
title:"Open"
subrole:"AXDialog"
name:"Open"
description:"dialog"
Personally, I'd probably rely on name and role description, which should be the same anytime an app throughs up a standard 'Open' dialog. 'Save' dialogs will be the same, except that title and name will be 'save' rather than 'open'.
If you have an app that presents a open or save sheet (a sub window attached to the titlebar), not a separate dialog, then you'll shift things a little. The AppleScript to get the properties looks like this:
tell application "anApp" to activate
delay 1
tell application "System Events"
tell process "anApp"
tell window 1
properties of sheet 1
end tell
end tell
end tell
and the relevant testable properties are as follows:
accessibility description:"save"
role description:"sheet"
role:"AXSheet"
description:"save"
You'll probably have to add logic to test whether the front window has a sheet, which should distinguish between dialogs and sheets.
Some apps use non-standard open/save dialogs, and you'll have to account for them on a case-by-case basis. There's no magic bullet for that.

Need script to click button on website that downloads a file

I am new to this so this is probably a dumb question but....
I am trying to get a download to happen off a website by clicking on a link but I don't think I have my code right for AppleScript.
The script opens the right website, but when I try to get it to download the file I need by clicking "export data" the code below doesnt seem to do anything, and am not sure what I am missing/did wrong. No error code. Just doesnt do anything.
Website Here
to clickId(LeaderBoard1_cmdCSV)
tell application "Safari"
do JavaScript "document.getElementById('" & LeaderBoard1_cmdCSV & "').click();" in document 1
end tell
end clickId
Thanks for the help.
The following example AppleScript code will open a new Safari window to the designated URL, wait for the page to finish loading, then click the Export Data link to download the FanGraphs Leaderboard.csv file.
Note: This was tested on macOS High Sierra, however for macOS Mojave and later there is a note in the waitForPageToFinishLoadingInSafari() handler to modify its code. Don't forget to do it if applicable.
To use JavaScript with AppleScript and Safari the Allow JavaScript from Apple Events on the Safari > Develop menu, which is hidden by default, must be checked. It can be shown by checking [√] Show Develop menu in menu bar in: Safari > Preferences… > Advanced
set theURL to "https://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=50&type=c%2c6%2c11%2c12%2c13%2c21%2c23%2c39%2c35%2c34%2c41%2c42%2c43%2c104%2c107%2c110%2c206%2c209%2c211%2c50%2c61&season=2019&month=0&season1=2019&ind=0&team=0&rost=0&age=0&filter=&players=0"
tell application "Safari" to ¬
make new document with properties {URL:theURL}
my waitForPageToFinishLoadingInSafari()
my clickId("LeaderBoard1_cmdCSV")
-- # Handlers:
to clickId(ElementID)
tell application "Safari"
do JavaScript "document.getElementById('" & ElementID & "').click();" in document 1
end tell
end clickId
on waitForPageToFinishLoadingInSafari()
-- # NOTE: For macOS Mojave and later, change 'UI element 1' to 'UI element 2` in the code below.
tell application "System Events"
repeat until (accessibility description of ¬
button 1 of UI element 1 of every group of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page") contains "Reload this page"
delay 0.5
end repeat
end tell
end waitForPageToFinishLoadingInSafari
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. 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.

Increase speed of applescript keystroke

I'm using applescript to automate some browser activity. I have it tied to the speech recognition, so that I can make some custom voice commands.
One such command is "I wanna go home", which when heard, pulls up the relevant bus schedule on my browser. Given that the public transit system uses PHP and GET, the URL gets pretty long. Therefore, the keystroke myURL in the applescript takes a while to execute (something like 1-2 seconds). While I can live with losing 1-2 seconds, I'd really rather not.
With that in mind, is it possible to send these keystrokes faster? I believe I read somewhere that using key code is faster than using keystroke, but for the life of me, I can't figure out where I read that.
EDIT: My browser of choice is Google Chrome, which I couldn't find URL hooks for. This is why I had to resort to using keystrokes. Therefore, I'd prefer answers that work with Chrome over Safari
Another way to solve this is to put the text in the clipboard, then paste it. You can save the clipboard contents first and put it back afterward, so you don't lose what's already there. This method works in other situations when you want to enter a lot of text, not just for browser URLs like the other answers.
set clipboard_contents to (the clipboard)
set the clipboard to "Some long string that would take a while with the keystroke method"
tell application "System Events" to keystroke "v" using command down
delay 0.2 -- needed because otherwise the next command can run before the paste occurs
set the clipboard to clipboard_contents
I'm pretty sure you can script your browser to open the URL directly, instead of typing in the keystrokes (faking input events in AppleScript should be considered a last resort).
You can do this with the open location standard addition:
open location "http://google.com"
Open location is defined in Standard Additions and should not be enclosed in a tell statement.
open location "http://google.com"
If you want to target a specific browser you can use:
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "http://google.com"
end tell
To target Chrome:
tell application "Google Chrome"
if not (exists window 1) then reopen
set URL of active tab of window 1 to "https://www.google.com/"
end tell

Get Source of Current Tab in Google Chrome via Applescript

It's child's play to do this in Safari, which has good Applescript support. Google Chrome's AS support has just arrived so I'm giving them the benefit of the doubt. I am basically trying to grab the current HTML via the clipboard so I can get information out. We have some nifty commands like this:
tell application "Google Chrome"
view source of active tab of window 1
save active tab of window 1
print active tab of window 1
reload active tab of window 1
go back active tab of window 1
go forward active tab of window 1
copy selection of active tab of window 1
paste selection active tab of window 1
end tell
but alas you can't say "set X to source of active tab of window 1". Anyone have any suggestions for me? My current ideas are to load the code I need in the background in Safari (pretty ugly) or try to display source and grab it with UI script, but that's also ugly. Also I keep encountering scripting bugs that keep it from working.
Any help would be appreciated.
Since google chrome supports Javascript
--Applescript code
tell active tab of window 1
set sourcehtml to execute javascript
document.getElementsByTagName('html')[0].innerHTML
end tell
Chrome's AppleScript library can execute JavaScript.
This will assign the full source of the page to source and return it
tell application "Google Chrome"
set source to execute front window's active tab javascript "document.documentElement.outerHTML"
end tell
I'm very excited to learn that Chrome has AppleScript support now. It's unfortunate that it's minimal as yet, but I'm sure (I hope!) it'll get better. Since there's no way to get the source directly, I'd choose the following hackish route:
tell application "Google Chrome"
view source of active tab of window 1 -- Or whichever tab you want
delay 3
repeat while loading of active tab of window 1
delay 3
end repeat
select all of active tab of window 1 -- Must *always* be the active tab
copy selection of active tab of window 1
delete tab (active tab index of window 1) of window 1
end tell
delay 1
return the clipboard
Yes, it's hackish, but that's unavoidable, given the current state of the scripting dictionary. The script should be straightforward: open a source tab, wait for it to load, select the contents, copy it, and close the tab. You can play with the delay 3s to see what works best. Note that the first active tab of window 1 is arbitrary, the rest explicitly refer to the source tab. Also, apparently there's no way to close a tab from within Chrome's scripting dictionary (oy vey), so I had to use JavaScript instead. Also, the last delay 1 shouldn't be necessary, but if it wasn't there, my tests would sometimes return the wrong thing, even though the clipboard contents were correct when I pasted them in. I think it's because there was enough text that it took a noticeable amount of time to update the clipboard.
Edit 1: I replaced execute the active tab of window 1 javascript "window.close()" with the delete tab line, as was suggested to me. Unfortunately, delete tab active tab of window 1 doesn't work, so you need this slightly more convoluted construction.
-- This script copies the HTML of a tab to a TextEdit document.
tell application "Chromium"
tell tab 1 of window 1 to view source
repeat while (loading of tab 2 of window 1)
end repeat
tell tab 2 of window 1 to select all
tell tab 2 of window 1 to copy selection
end tell
tell application "TextEdit"
set text of document 1 to the clipboard
end tell
Explanation: The script is put in a tight loop waiting for the tab to load, Then just copies the HTML to clipboard.
tell application "Google Chrome"
set t to active tab index of window 1
tell active tab of window 1 to view source
set t to t + 1
repeat while (loading of tab t of window 1)
end repeat
tell tab t of window 1 to select all
tell tab t of window 1 to copy selection
delete tab t of window 1
end tell
EDIT1: the above script should do exaclt what you want
The simple answer is:
set sourcehtml to execute javascript "document.getElementsByTagName('html')[0].innerHTML"
Seems, other posts are really close but still no clear/working solution. The code working to me:
tell application "Google Chrome"
activate
tell active tab of window 1
set sourcehtml to execute javascript "document.getElementsByTagName('html')[0].innerHTML"
return sourcehtml
end tell
end tell
Seeing as how Chrome's AS support has "just arrived" it is bound to be "exciting" to use. In trying some of the commands they have available in their dictionary, it looks as though they still have some kinks to work out. Until Google exposes a way in the API to get the source code more easily (and/or works out the related kinks), you'll have to use one of the alternatives you mention in your post.

Resources