Automation 尝试在登录时将新文件夹名称打印到文本编辑/数字文件(AppleScript)

Automation 尝试在登录时将新文件夹名称打印到文本编辑/数字文件(AppleScript),automation,directory,applescript,Automation,Directory,Applescript,我正在尝试将添加到驱动器的新文件夹添加到文本编辑或数字文件中,以便在各个驱动器上有活动项目的最新列表 我对AppleScript非常陌生,所以我不知道我的限制是什么,但我基本上只需要弄清楚如何用新文件夹的名称附加到文件的末尾(似乎textEdit最简单)。我目前有: on adding folder items to theAttachedFolder after receiving theNewItems -- Get the name of the attached folder tell

我正在尝试将添加到驱动器的新文件夹添加到文本编辑或数字文件中,以便在各个驱动器上有活动项目的最新列表

我对AppleScript非常陌生,所以我不知道我的限制是什么,但我基本上只需要弄清楚如何用新文件夹的名称附加到文件的末尾(似乎textEdit最简单)。我目前有:

on adding folder items to theAttachedFolder after receiving theNewItems
-- Get the name of the attached folder
tell application "Finder"
    set theName to name of theAttachedFolder
    
    -- Count the new items
    set theCount to length of theNewItems
    
    -- Display an alert indicating that the new items were received
    activate
    display alert "Attention!" message (theCount & " new items were detected in folder. Adding to TextEdit file.")
        
    end repeat
end tell

非常感谢您的帮助。谢谢大家!

StandardAdditions脚本字典中的文件读/写套件具有
打开访问
写入
命令,可用于附加到文本文件(还具有命令引用),AppleScript的
文本项分隔符
或字符串连接可用于从文件项列表中组合字符串

有一些描述写入文件的方法,但在下面的示例中,我将主要内容与文件夹操作处理程序分离,以便也可以从
run
处理程序调用它,以便在脚本编辑器中进行测试:

property logFile : "/path/to/output/textfile.txt" -- HFS or POSIX

on run -- test
   set folderItems to (choose file with multiple selections allowed)
   tell application "Finder" to set theFolder to container of first item of folderItems
   doStuff(theFolder, folderItems)
end run

on adding folder items to theAttachedFolder after receiving theNewItems
   doStuff(theAttachedFolder, theNewItems)
end adding folder items to

to doStuff(theFolder, theItems)
   set folderItems to ""
   activate
   display alert "Attention!" message "" & (count theItems) & " new items were detected in folder." & return & "Adding to log file."
  tell application "Finder" to set folderName to name of theFolder
  repeat with anItem in theItems
      tell application "Finder" to set newName to name of anItem
      set folderItems to folderItems & tab & newName & return
   end repeat
   writeToFile(logFile, "Items added to " & folderName & ":" & return & folderItems & return)
end doStuff

to writeToFile(someFile, someText)
   try
      set theOpenFile to (open for access someFile with write permission)
      write someText to theOpenFile starting at eof -- append
      close access theOpenFile
   on error -- make sure file is closed
      try
         close access theOpenFile
      end try
   end try
end writeToFile