2015-07-20 83 views
0

這似乎是這樣一個容易的事情做,我很驚訝的解決方案仍然躲過我。AppleScript選擇文件對話框存儲在文本文件中打開的最後一個文件夾。

我試圖存儲由「選擇文件」對話框打開的最後一個文件夾。我想將該文件夾的位置存儲在文本文件中。

我想讓我的「選擇文件」對話框總是打開到最後使用的文件夾。

我有很多的腳本工作,但有一個奇怪的事情,不斷逃避我。

看我的劇本......

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」。

然後我使用配置文件中的值設置strPath。

我顯示新值,它是我的系統上的有效位置。

接下來,我嘗試了對話,我得到的「文件別名的Macintosh HD的錯誤信息:用戶:lowken:中未找到文件

讓改變腳本,這樣,而不是使用值從文件的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.

我在2012年年中的MacBook Pro上運行OS X Yosemite 10.10.4。

回答

1

問題是你的「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」 文檔的編碼。

+0

我認爲可能就是這樣。至少它對我有意義。 – codingguy3000

+0

這樣做。如何在世界上所有的好名字中輸入«&»符號?我能夠複製和粘貼它來運行,但我很想知道如何鍵入這些符號。 – codingguy3000

+0

這取決於鍵盤。在** U.S。**鍵盤上: 按** Shift + \鍵**鍵入左側雙角括號。 按** Option + Shift + \ keys **鍵入右側雙角括號。 – jackjr300

1

choose file命令的默認位置可以是Unix路徑「/ Users/my_user/Documents」或者之前有Alias的Finder路徑,如:alias「HD:Users:my_User:Documents」 所以首先檢查strPath是正確的值,然後如果OK,仔細檢查之類的吧:

Display Dialog (class of strPath) as string 

它可能不是OK,你必須強迫的(路徑主文件夾爲字符串),而不是文字。

+0

我的直覺告訴我,當我從「選擇文件」不喜歡的文本文件中提取值時,有關於數據類型的信息。我會嘗試轉換爲字符串(儘管我認爲我已經嘗試過了)。 – codingguy3000

+0

在你的第一個腳本中,你將路徑指定爲「文本」......將其指定爲「字符串」 – pbell

相關問題