ApplescriptObjC - 从Finder读取普通txt文件的内容并将其作为列表

时间:2016-08-25 12:18:45

标签: applescript applescript-objc

我正在尝试读取txt文件的内容(IP地址列表),然后从中创建一个列表以在shell脚本循环中使用。似乎是错误的"设置切换到段落(读取POSIX文件文件路径)"而且我认为我需要重建,但我知道Applescript并且几乎没有进入ObjC所以这个让我难过......

我真的想改变前三行来设置文件路径来选择文件,但是我得到了同样的错误。

on theButtonChooseFile_(sender)
    set x to (system info)
    set theuser to short user name of x
    set filepath to "/Users/" & theuser & "/Desktop/switches.txt" as string -- Define path to file
    set listOfSwitches to {}
    set Switches to paragraphs of (read POSIX file filepath)
    repeat with nextLine in Switches
        if length of nextLine is greater than 0 then
            copy nextLine to the end of listOfSwitches
        end if
    end repeat
end theButtonChooseFile_

错误:

  

2016-08-25 08:13:37.655 Cisco Configurator [67884:4593159] *** - [AppDelegate theButtonChooseFile:]:无法将当前应用程序转换为类型文件。 (错误-1700)

1 个答案:

答案 0 :(得分:0)

AppleScript read命令也接受POSIX路径,您可以使用

获取当前用户的桌面文件夹的路径
path to desktop

可以用这种方式编写整个处理程序

on theButtonChooseFile_(sender)
    set desktopFolder to POSIX path of (path to desktop)
    set filepath to desktopFolder & "switches.txt"
    set Switches to paragraphs of (read filepath)

    set listOfSwitches to {}
    repeat with nextLine in Switches
        if length of nextLine is greater than 0 then
            copy nextLine to the end of listOfSwitches
        end if
    end repeat
end theButtonChooseFile_

但请注意,listOfSwitches只能在处理程序中访问