曾几何时,可以将file:// url放入网页,如果该网址与桌面上的文件匹配,为什么,当您点击链接时,该文件会在您的计算机上打开。
出于安全原因,此功能已被禁用,但我尝试重新创建它以供我个人使用。我试图使用http://www.macosxautomation.com/applescript/linktrigger/中描述的自定义URL协议和Applescript应用程序。我几乎让它工作,但有一个困难:网址中不能有空格,因此它们会被转义,其他特殊字符就像"&"一样。我怎样才能说服Applescript打开一个带有转义字符的路径的文件,例如" /Users/jim/Dropbox/Getting%20Started.pdf"?
tell application "Finder"
open "/Users/jim/Dropbox/Getting Started.pdf" as POSIX file
工作正常,而
tell application "Finder"
open "/Users/jim/Dropbox/Getting%20Started.pdf" as POSIX file
失败。
是否有一种简单(即非正则表达式)的方法来完成这项工作?
答案 0 :(得分:3)
您可以使用do shell script
中的打开命令。
像这样:
set tUrl to "/Users/jim/Dropbox/Getting%20Started.pdf"
do shell script "open 'file://" & tUrl & "'"
答案 1 :(得分:1)
尝试以下方法:
tell application "Finder"
open my decodeFromUri("/Users/jim/Dropbox/Getting%20Started.pdf") as POSIX file
end tell
声明以下处理程序后:
(*
Decodes a string previously encoded for inclusion in a URI (URL).
Note: Uses Perl and its URI::Escape module (preinstalled as of at least OSX 10.8).
Adapted, with gratitude, from http://applescript.bratis-lover.net/library/url/
Example:
my decodeFromUri("me%2Fyou%20%26%20Mot%C3%B6rhead") # -> "me/you & Motörhead"
*)
on decodeFromUri(str)
if str is missing value or str = "" then return str
try
# !! We MUST use `-ne` with `print` rather than just `-pe`; the latter returns the input unmodified.
return do shell script "printf '%s' " & quoted form of str & " | perl -MURI::Escape -ne 'print uri_unescape($_)'"
on error eMsg number eNum
error "Decoding from URI failed: " & eMsg number eNum
end try
end decodeFromUri