使用Applescript打开指定文件夹下的文件

时间:2017-07-16 06:05:35

标签: applescript filepath

假设我有一堆具有类似子路径的文件夹:

Folder 1
  Do
    Re
      Mi
        <Files>
Folder 2
  Do
    Re
      Mi
        <Different Files>

我知道我可以使用tell application Finder to open "Macintosh HD:Users: ...打开文件夹。是否有任何方法可以将弹出窗口排队以选择我要输入的Folder 1Folder 2中的哪一个,然后将其输入上述命令?例如,如果我选择Folder 1,则会转到...Folder 1/Do/Re/Mi,而如果我选择Folder 2,则转到...Folder 2/Do/Re/Mi

我尝试过的一件事是将do shell script与concatenate结合起来以获取do shell script "open " & variable & "/Do/Re/Mi/",但如果文件名不止一个,则代码会失败。

1 个答案:

答案 0 :(得分:0)

  • 设置基本文件夹
  • 检索基础文件夹的所有文件夹名称
  • 创建列表
  • 选择项目
  • 连接路径并打开文件夹

例如(将baseFolder设置为Folder 1Folder 2的父文件夹的别名

set baseFolder to path to home folder
set folderList to {}
tell application "Finder" to set allFolders to name of folders of baseFolder
repeat with i from 1 to (count allFolders)
    set end of folderList to (i as text) & space & item i of allFolders
end repeat
set chosenItem to choose from list folderList
if chosenItem is false then return
set chosenIndex to word 1 of item 1 of chosenItem
set selectedFolder to item chosenIndex of allFolders
set destinationFolder to (baseFolder as text) & selectedFolder & ":Do:Re:Mi:"

tell application "Finder"
    if exists folder destinationFolder then
        open folder destinationFolder
    else
        display dialog "Sorry, the folder does not exist"
    end if
end tell