我想下载一个txt文件,该文件使用AppleScript在chrome标签中打开。我希望Mac的“另存为”对话框提供默认扩展名和文件名。
tell application "Google Chrome" to tell active tab of window 1 to save as "r1.txt"
我尝试了这种方法,还有其他一些方法,例如
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
仍然无法单击模式中的“保存”按钮。
答案 0 :(得分:0)
使用最新版本的Google Chrome浏览器和最新版本的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
答案 1 :(得分:0)
我对这个问题的解决方法是尝试避免在可能的情况下脚本脚本UI,这可能是有问题且不可靠的。相反,我决定使用shell命令curl
为我们完成下载工作,而不是尝试操纵 Chrome 来完成它。
我们所需要做的只是将文件保存到的位置,我将其设置为 Google Chrome 默认的位置,即~/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
这是一个比您现在的脚本更长的脚本,但是其中大多数是变量(和属性)的声明,此后脚本执行了两个简单的操作:
获取 Chrome 中活动选项卡的URL,并将该URL的内容下载到指定的文件夹中,并保留与远程文件相同的文件名和扩展名;
下载完成后,它将在 Finder 中显示文件。