尝试在记录时将新文件夹名称打印到文本 edit/numbers 文件 (AppleScript)
Trying to print new folder names to a text edit/numbers file when logged (AppleScript)
我正在尝试将添加到驱动器的新文件夹添加到 textEdit 或数字文件中,以便有一个最新列表,列出我在各个驱动器上的活动项目。
我是 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
非常感谢任何帮助。谢谢!
File Read/Write 套件在 StandardAdditions 脚本词典中有 open for access
和 write
可用于附加到文本文件的命令(AppleScript Language Guide 也有命令参考),AppleScript 的 text item delimiters
或字符串连接可用于 assemble 列表中的字符串文件项。
有一些 older topics 描述了写入文件的方法,但在下面的示例中,我将主要内容与文件夹操作处理程序分开,以便它也可以从 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
我正在尝试将添加到驱动器的新文件夹添加到 textEdit 或数字文件中,以便有一个最新列表,列出我在各个驱动器上的活动项目。
我是 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
非常感谢任何帮助。谢谢!
File Read/Write 套件在 StandardAdditions 脚本词典中有 open for access
和 write
可用于附加到文本文件的命令(AppleScript Language Guide 也有命令参考),AppleScript 的 text item delimiters
或字符串连接可用于 assemble 列表中的字符串文件项。
有一些 older topics 描述了写入文件的方法,但在下面的示例中,我将主要内容与文件夹操作处理程序分开,以便它也可以从 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