我正在尝试制作Droplet applscript应用。应该这样做:
当我检测到正确的文件并且是否尝试打开它时,我收到此错误(应用程序将无法启动 - 之前我收到此错误并且脚本取消):class nmxt of alias "the path to the file" could not be read
目前我的剧本:
on open {input}
set theFiles to (getFilesRecursively(input, "plhs"))
repeat with oneFile in theFiles
if name extension of oneFile is "plhs" then
tell application "Applic"
open oneFile
activate
tell application "System Events"
tell process "Applic"
click menu item "Save" of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
end tell
end if
end repeat
end open
on getFilesRecursively(fContainer, fExt)
tell application "Finder"
set recursiveFileList to entire contents of fContainer as alias list
set resultFileList to {}
repeat with aFile in recursiveFileList
if name extension of aFile contains fExt then
set resultFileList to resultFileList & aFile
end if
end repeat
end tell
return resultFileList
end getFilesRecursively
答案 0 :(得分:0)
这是一个应该让你前进的生发剧本:
property kTargetFileExtension : "txt"
property pValidFileList : {}
on open of theFiles -- Executed when files or folders are dropped on the script
set fileCount to (get count of items in theFiles)
repeat with thisFile from 1 to fileCount
set theFile to item thisFile of theFiles
tell application "System Events"
set file_info to get info for theFile
end tell
if visible of file_info is true then -- check for the file extension here as well
if folder of file_info is true then
my createList(theFile)
else
set fileName to name of file_info
set targetFileFound to isTargetFile(fileName, kTargetFileExtension) of me
if (targetFileFound) then
set end of pValidFileList to theFile
end if
end if
end if
end repeat
display dialog "pValidFileList = " & pValidFileList
(* do something with your files listed in pValidFileList here *)
end open
on createList(mSource_folder)
set item_list to ""
tell application "System Events"
set item_list to get the name of every disk item of (mSource_folder as alias)
end tell
set item_count to (get count of items in item_list)
repeat with i from 1 to item_count
set the_properties to ""
set the_item to item i of the item_list
set fileName to the_item
set the_item to ((mSource_folder & the_item) as string) as alias
tell application "System Events"
set file_info to get info for the_item
end tell
if visible of file_info is true then -- check for the file extension here as well
if folder of file_info is true then
my createList(the_item)
else
set targetFileFound to isTargetFile(fileName, kTargetFileExtension) of me
if (targetFileFound) then
set end of pValidFileList to the_item
end if
end if
end if
end repeat
end createList
on isTargetFile(theFilename, theTargetExtension) -- (string, string) as boolean
set AppleScript's text item delimiters to "."
set fileNameList to every text item of theFilename
set AppleScript's text item delimiters to ""
try
set theFileExtension to item 2 of fileNameList as string
on error
return false
end try
if theFileExtension is theTargetExtension then
return true
end if
return false
end isTargetFile
有几点需要注意:
系统事件是获取文件列表和信息的当前最佳实践。只是要求整个内容更快,但已知不可靠。这种手动抓取方法较慢,但毫无疑问,您将获得所需的文件。
isTargetFile
实际上只是将文件名作为字符串使用,而不是依靠系统来提供信息。如果你问我,其中六个,另外一半,但这确实减少了对系统的调用次数,所以我想它会让它更快一点。
我还倾向于在这些内容中添加on run {}
块以允许手动选择文件夹。这样做也有助于测试。
如何使用:
将脚本保存为应用程序,您应该获得一个Droplet(带箭头的Applescript应用程序图标)。
on open of theFiles
相当于main()
。您可以将文件和文件夹的任意组合放到Droplet上,它将处理其余部分。它最终会有一个目标文件列表,您可以循环处理这些文件。我会把它作为练习让你加入那一点。
要自定义目标,请将第一行property kTargetFileExtension : "txt"
中的字符串更改为您要查找的扩展名。这也可以更改为数组 - 例如property kTargetFileExtension : {"txt", "rtf", "doc}
- 但您还需要更新isTargetFile(theFilename, theTargetExtension)
以循环使用这些数组。
除此之外,这才有效。现在,它将收集txt
个文件列表进行处理。
加盐调味。