Reputation: 1
I want to download a txt file, opened in a tab in chrome using AppleScript. I want the save as dialog of Mac to provide the default extension and the name of the file.
tell application "Google Chrome" to tell active tab of window 1 to save as "r1.txt"
I tried this approach, and some other approaches like
activate application "Google Chrome"
tell application "System Events"
tell process "chrome"
keystroke "s" using {command down}
delay 1
click button "Save" of sheet 1 of window 1
end tell
end tell
still am not able to click the save button in the modal.
Upvotes: 0
Views: 1027
Reputation: 6092
My approach to this problem was to try and avoid scripting the UI if possible, which can be problematic and unreliable. Instead, I decided to use the shell command curl
to do the downloading job for us instead of trying to manipulate Chrome into doing it.
All we need is the location of where to save the file to, which I've set as the location to which Google Chrome defaults to, namely ~/Downloads
.
property path : "~/Downloads" -- Where to download the file to
use Chrome : application "Google Chrome"
property sys : application "System Events"
property window : a reference to window 1 of Chrome
property tab : a reference to active tab of my window
property URL : a reference to URL of my tab
property text item delimiters : {space, "/"}
on run
-- Stop the script if there's no URL to grab
if not (my URL exists) then return false
-- Path to where the file will be saved
set HFSPath to the path of sys's item (my path)
-- Dereferencing the URL
set www to my URL as text
-- Extract the filename portion of the URL
set filename to the last text item of www
-- The shell script to grab the contents of a URL
set sh to the contents of {¬
"cd", quoted form of the POSIX path of HFSPath, ";", ¬
"curl --remote-name", ¬
"--url", quoted form of www} as text
## 1. Download the file
try
using terms from scripting additions
do shell script sh
end using terms from
on error E
return E
end try
## 2. Reveal the downloaded file in Finder
tell application "Finder"
tell the file named filename in the ¬
folder named HFSPath to if ¬
it exists then reveal it
activate
end tell
end run
It's a longer script than your present one, but most of it is declarations of variables (and properties), after which the script does two simple things:
Grabs the URL of the active tab in Chrome, and downloads the contents of that URL into the specified folder, retaining the same filename and extension as the remote file;
Once the download is complete, it reveals the file in Finder.
Upvotes: 0
Reputation: 3142
This works for me using the latest version of Google Chrome and the latest version of MacOS Mojave
activate application "Google Chrome"
tell application "System Events"
repeat while not (exists of menu bar item "File" of menu bar 1 of application process "Chrome")
delay 0.1
end repeat
click menu bar item "File" of menu bar 1 of application process "Chrome"
repeat while not (exists of menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome")
delay 0.1
end repeat
click menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome"
repeat while not (exists of UI element "Save" of sheet 1 of window 1 of application process "Chrome")
delay 0.1
end repeat
click UI element "Save" of sheet 1 of window 1 of application process "Chrome"
end tell
Upvotes: 1