无法获取/ path /的每个文件夹

时间:2020-08-14 23:06:11

标签: applescript

我写的代码是

  set localaccount to {"1", "2"}
set locacc to choose from list localaccount with prompt "Select your Account Location:" default items {"1"}


set accounstlist to POSIX path of thisFolder & "accountlist.txt"
set folderloc to POSIX path of thisFolder & locacc
tell application "System Events"
    set applicationNames to name of folders of folderloc
end tell
set {TID, text item delimiters} to {text item delimiters, linefeed}
set namesText to applicationNames as text
set text item delimiters to TID
try
    set fileDescriptor to open for access accounstlist with write permission
    write namesText to fileDescriptor
    close access fileDescriptor
on error e number n
    try
        close access file accounstlist
    end try
    display dialog "Error: " & e & " - number: " & n buttons {"Cancel"} default button "Cancel"
end try
set theaccounts to paragraphs of (read accounstlist)
set theaccount to choose from list theaccounts with prompt "Select Account You Want To Export"

但是在运行时它给了我无法获取/ path /的每个文件夹的信息 路径是“ folderloc” 谁能帮我吗?

2 个答案:

答案 0 :(得分:1)

我设法通过更改如下代码来解决该问题:

set accounstlist1 to POSIX path of thisFolder & "accountlist.txt"
    set accounstlist1 to POSIX file accounstlist1
    set accounstlist to accounstlist1 as alias
    set folderloc1 to POSIX path of thisFolder & locacc
    set folderloc1 to POSIX file folderloc1
    set folderloc to folderloc1 as alias

问题与路径有关。 感谢您的帮助。

答案 1 :(得分:1)

为自己解决问题做得很好。这个问题几乎可以肯定是在系统事件块中,您试图从folderloc中检索文件夹列表。在您的原始脚本中,folderloc只是一个包含纯文本posix路径的字符串,因此当您要求系统事件从一段文本中获取文件夹时,这没有任何意义。您的编辑通过将posix路径转换为POSIX file然后转换为alias来解决了这一问题,系统事件随后可以访问该路径以获取文件夹列表。

这是脚本的正确版本,其中消除了不必要的大量多余脂肪:

set localaccount to {"1", "2"}
set locacc to choose from list localaccount with prompt "Select your Account Location:" default items {"1"}
set accounstlist to POSIX path of thisFolder & "accountlist.txt"
set folderloc to POSIX path of thisFolder & locacc
set text item delimiters to linefeed
tell application "System Events" to set namesText to ¬
        (name of folders of folder folderloc) as text
write namesText to accounstlist
set theaccounts to paragraphs of (read accounstlist)
set theaccount to choose from list theaccounts with prompt "Select Account You Want To Export"