AppleScript 选择文件对话框存储在文本文件中打开的最后一个文件夹。

AppleScript choose file dialog box store last folder opened in text file.

这似乎是一件很容易做到的事情,令我惊讶的是我仍然找不到解决方案。

我正在尝试存储由 "choose file" 对话框打开的最后一个文件夹。我想将该文件夹的位置存储在文本文件中。

我想让我的 "choose file" 对话框始终打开到上次使用的文件夹。

我有很多脚本在工作,但有一件奇怪的事情一直在躲避我。

看看我的脚本...

set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")

set strPathFromConfig to item 1 of Shows as string

set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)

display dialog strPathFromConfig

set strPath to (path to home folder as text) & strPathFromConfig

display dialog strPath

choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath

脚本读取我的配置文本文件,其中包含一行且只有一串 "Documents"。

我 trim 一些前导垃圾字符并显示一个对话框,结果为 "Documents"。

然后我使用配置文件中的值设置 strPath。

我显示新值是因为它是我系统上的有效位置。

接下来我尝试打开对话框,但收到错误消息“未找到文件别名 Macintosh HD:Users:lowken:Documents。

让我们更改脚本,而不是使用从 config.txt 文件中提取的值,我只是在我的脚本中设置一个字符串变量。

set Shows to paragraphs of (read POSIX file "/Users/lowken/Documents/config.txt")

set strPathFromConfig to item 1 of Shows as string

set strPathFromConfig to ((characters 3 thru -1 of strPathFromConfig) as string)

display dialog strPathFromConfig

set strTemp to "Documents"

set strPath to (path to home folder as text) & strTemp

display dialog strPath

choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location alias strPath

现在可以了。 AppleScript 似乎不想使用从 config.txt 文件中查找的值。

我做错了什么?我尝试投射到一个别名我在我的系统上尝试了不同的位置。

在我看来,不知何故从文本文件中查找的值不是字符串数据类型。

有什么想法吗?

P.S。

我是 运行 OS X Yosemite 10.10.4,使用的是 2012 年中期的 MacBook Pro。

选择文件命令的默认位置可以是 Unix 路径“/Users/my_user/Documents”或前面带有别名的 Finder 路径,例如:alias "HD:Users:my_User:Documents" 所以首先检查 strPath 是否是正确的值,然后如果确定,再检查它的 class :

Display Dialog (class of strPath) as string

这可能不行,您必须强制(主文件夹的路径作为字符串)而不是文本。

问题出在您的 "config.txt" 文档的编码上。

read命令可以读取(MacRomanUTF-8UTF-16) 编码的文本文件,但必须指定类型 class,否则它会将文本文件读取为 MacRoman。

set strPathFromConfig to paragraph 1 of (read "/Users/lowken/Documents/config.txt" as «class ut16») -- or if UTF-8 use —> as «class utf8»
set strPath to ((path to home folder as text) & strPathFromConfig) as alias
choose file with prompt "Please choose a file:" of type {"XLSX", "APPL"} default location strPath

对于其他编码,您必须更改“config.txt”文档的编码。