0

我遇到了一个奇怪的 AppleScript 问题,似乎无法找出根本原因或解决方案。我正在尝试使用模仿以下过程的过程下载文件。此代码对我来说失败了,报告“ URL 访问脚本出错:无法将某些数据转换为预期的类型。” 编号 -1700 到项目

如果我从本地路径名中删除 3 个字符(这三个字符似乎无关紧要),那么它运行良好。如果我只删除 2 个字符,它不会引发错误,但它下载的文件是损坏的 JPG。我尝试在“do shell script”行中对 curl 执行相同的操作,但对于任何文件名长度,它似乎也失败了,尽管是默默地(返回"")。是什么原因造成的,我能做些什么呢?

tell application "URL Access Scripting"
    download "http://interfacelift.com/wallpaper_beta/grab/02331_veiledinclouds_2560x1600.jpg" to "/Users/abc/Downloads/02331_veiledinclouds_2560x1600.jpg"
end tell
4

3 回答 3

1

我敢打赌这个函数仍然调用一些只允许 31 个字符的古老的 Carbon(或 pre-Carbon)API 调用。一些与此相关的 Google 链接可以追溯到 2003 年,就 Mac OS 而言,年份和特定的 31 个字符限制都表明这是一个过时的 API,从未更新过。这是 Applescript 较暗角落的问题。也许 Satimage 或某人已经做了一个脚本添加来解决这个问题。我认为你被困住了,需要从这个添加之外的某个地方获得帮助。我很想证明这一点是错误的,因为无论我尝试什么,我都无法让它发挥作用。

于 2010-08-10T01:06:14.710 回答
1

Your biggest problem is you're using a posix-style path (using slashes) when you should be using a mac-style path (with colons). Applescript uses colon delimited paths. We can convert between the two using "posix path" to go from colons to slashes and "posix file" to go from slashes to colons.

And you can't just pass the path as a string. In applescript we use file specifiers... which is why I put the word file in front of the string path. So this works once we fix that.

set posixPath to "/Users/abc/Downloads/02331_veiledinclouds_2560x1600.jpg"
set macPath to (POSIX file posixPath) as text

tell application "URL Access Scripting"
    download "http://interfacelift.com/wallpaper_beta/grab/02331_veiledinclouds_2560x1600.jpg" to file macPath
end tell

However, there must be a filename length issue because when I run it the filename of the downloaded file is shortened to 31 characters.

EDIT: Here's a script to truncate, download, and rename the file if necessary.

set posixPath to "/Users/abc/Downloads/02331_veiledinclouds_2560x1600.jpg"
set baseName to do shell script "/usr/bin/basename " & quoted form of posixPath

set needsRenaming to false
if (count of baseName) is greater than 31 then
    set downloadName to text -31 thru -1 of baseName
    set basePath to do shell script "/usr/bin/dirname " & quoted form of posixPath
    set posixPath to basePath & "/" & downloadName
    set needsRenaming to true
end if

set macPath to (POSIX file posixPath) as text

tell application "URL Access Scripting"
    download "http://interfacelift.com/wallpaper_beta/grab/02331_veiledinclouds_2560x1600.jpg" to file macPath
end tell

if needsRenaming then
    tell application "Finder"
        set name of file macPath to baseName
    end tell
end if
于 2010-08-09T21:23:13.460 回答
1

我最终决定下载一个临时的、较短的名称,然后用 Finder 重命名它。重写后的脚本如下:

tell application "URL Access Scripting"
    set tempFileName to "abc.jpg"
    set downloadPath to (POSIX path of (path to downloads folder))

    set tempFile to download "http://interfacelift.com/wallpaper_beta/grab/02331_veiledinclouds_2560x1600.jpg" to downloadPath & tempFileName
end tell

tell application "Finder" to set name of file tempFile to "02331_veiledinclouds_2560x1600.jpg"

我更喜欢这种方法的简单性,而不是 regulus6633 的方法,它只在需要时重命名文件。

于 2010-08-10T12:32:47.407 回答