In Applescript how do I read a website, and output the text as a variable?
在Applescript中如何阅读网站,并将文本输出为变量?
I want to read the "Latest" number from here, then use that number to download the latest version of Chromium from here.
我想从这里读取“最新”号码,然后使用该号码从这里下载最新版本的Chromium。
3 个解决方案
#1
Not a direct Applescript way, but this works well.
不是直接的Applescript方式,但这很好用。
Create a shell script to do the download, say chrome-download.sh in your $HOME/bin directory:
创建一个shell脚本来进行下载,比如说$ HOME / bin目录中的chrome-download.sh:
#!/bin/sh
BUILD=`curl http://build.chromium.org/buildbot/snapshots/sub-rel-mac/LATEST`
echo "Downloading build "$BUILD
curl http://build.chromium.org/buildbot/snapshots/sub-rel-mac/$BUILD/chrome-mac.zip -o $HOME/chrome-mac-$BUILD.zip
Run it from Applescript (or Automator) in one line:
从Applescript(或Automator)中以一行运行它:
do shell script "/bin/bash /Users/<your username>/bin/chrome-download.sh"
The downloaded file lands in your $HOME directory. The bash script will work from Linux or Cygwin, as well. Of course, you could just run the bash script directly, too.
下载的文件位于$ HOME目录中。 bash脚本也可以在Linux或Cygwin中运行。当然,您也可以直接运行bash脚本。
#2
As Glenn said it's much easier to just use a shell script and possibly wrap it in AppleScript by using do shell script
. Another alternative if you want a GUI of sorts to the script is to look at a program called Platypus.
正如Glenn所说,使用shell脚本并使用do shell脚本将其包装在AppleScript中要容易得多。如果你想要一个对脚本进行分类的GUI,另一种选择是查看一个名为Platypus的程序。
Lastly if you're looking for a sample script that already does this I made one when Chromium Mac was announced a couple days back: http://chealion.ca/2009/05/chromium-build-bot-updater-script/ (Source on GitHub).
最后,如果您正在寻找已经执行此操作的示例脚本,那么我会在几天前宣布Chromium Mac时制作一个脚本:http://chealion.ca/2009/05/chromium-build-bot-updater-script/(来自GitHub)。
#3
A more AppleScript-native way would be:
AppleScript原生的方式更多:
set filePath to (path to temporary items folder as string) & "file.html"
tell application "URL Access Scripting"
download "http://www.apple.com/sitemap/" to file filePath replacing yes
end tell
--read file into a variable
try
open for access (file filePath)
set fileData to (read file filePath)
close access (file filePath)
on error errMsg number errNum
try
close access file filePath
end try
error errMsg number errNum
end try
You can find more information in the URL Access Scripting and StandardAdditions dictionaries. (File > Open Dictionary)
您可以在URL Access Scripting和StandardAdditions词典中找到更多信息。 (文件>打开字典)
#1
Not a direct Applescript way, but this works well.
不是直接的Applescript方式,但这很好用。
Create a shell script to do the download, say chrome-download.sh in your $HOME/bin directory:
创建一个shell脚本来进行下载,比如说$ HOME / bin目录中的chrome-download.sh:
#!/bin/sh
BUILD=`curl http://build.chromium.org/buildbot/snapshots/sub-rel-mac/LATEST`
echo "Downloading build "$BUILD
curl http://build.chromium.org/buildbot/snapshots/sub-rel-mac/$BUILD/chrome-mac.zip -o $HOME/chrome-mac-$BUILD.zip
Run it from Applescript (or Automator) in one line:
从Applescript(或Automator)中以一行运行它:
do shell script "/bin/bash /Users/<your username>/bin/chrome-download.sh"
The downloaded file lands in your $HOME directory. The bash script will work from Linux or Cygwin, as well. Of course, you could just run the bash script directly, too.
下载的文件位于$ HOME目录中。 bash脚本也可以在Linux或Cygwin中运行。当然,您也可以直接运行bash脚本。
#2
As Glenn said it's much easier to just use a shell script and possibly wrap it in AppleScript by using do shell script
. Another alternative if you want a GUI of sorts to the script is to look at a program called Platypus.
正如Glenn所说,使用shell脚本并使用do shell脚本将其包装在AppleScript中要容易得多。如果你想要一个对脚本进行分类的GUI,另一种选择是查看一个名为Platypus的程序。
Lastly if you're looking for a sample script that already does this I made one when Chromium Mac was announced a couple days back: http://chealion.ca/2009/05/chromium-build-bot-updater-script/ (Source on GitHub).
最后,如果您正在寻找已经执行此操作的示例脚本,那么我会在几天前宣布Chromium Mac时制作一个脚本:http://chealion.ca/2009/05/chromium-build-bot-updater-script/(来自GitHub)。
#3
A more AppleScript-native way would be:
AppleScript原生的方式更多:
set filePath to (path to temporary items folder as string) & "file.html"
tell application "URL Access Scripting"
download "http://www.apple.com/sitemap/" to file filePath replacing yes
end tell
--read file into a variable
try
open for access (file filePath)
set fileData to (read file filePath)
close access (file filePath)
on error errMsg number errNum
try
close access file filePath
end try
error errMsg number errNum
end try
You can find more information in the URL Access Scripting and StandardAdditions dictionaries. (File > Open Dictionary)
您可以在URL Access Scripting和StandardAdditions词典中找到更多信息。 (文件>打开字典)