查找并替换 AppleScript 不写入文件
Find and replace AppleScript not writing file
我第一次尝试制作 AppleScript,以便快速更改 ics 文件中的所有事件标题。我正在使用 Automator 通过简单的“运行 AppleScript”服务来执行此操作。
当我右键单击 ics 文件并激活该服务时,它 运行 没有任何更改。当我从 Automator 运行 它只是 returns false,我认为这意味着它无法写入文件。
on run {inputfile}
set filedata to inputfile as string
set filedata to replaceText("Title 1", "Title 2", filedata)
writeTextToFile(filedata, inputfile, true)
end run
on replaceText(find, replace, textString)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set textString to text items of textString
set AppleScript's text item delimiters to replace
set textString to "" & textString
set AppleScript's text item delimiters to prevTIDs
return textString
end replaceText
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
问题出在第2行吗?我认为您不需要指定文件路径,因为服务应该正在输入文件。
使用 macOS High Sierra 并将 Automator Service 设置为 在 [Finder] 和单个 运行 AppleScript action 中接收选定的 [文件或文件夹],以下 example AppleScript code 为我修改 ICS 文件:
- 请注意,按照编码,它可以处理多个选定的文件,并且只能修改有效的 ICS files。
on run {input, parameters}
repeat with anItem in input
set thisFile to anItem
if (get {folder, kind} of (info for thisFile)) is {false, "ICS file"} then
set fileData to (read thisFile)
set newFileData to replaceText("Title 1", "Title 2", fileData)
writeTextToFile(newFileData, thisFile, true)
end if
end repeat
end run
on replaceText(find, replace, textString)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set textString to text items of textString
set AppleScript's text item delimiters to replace
set textString to "" & textString
set AppleScript's text item delimiters to prevTIDs
return textString
end replaceText
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
我第一次尝试制作 AppleScript,以便快速更改 ics 文件中的所有事件标题。我正在使用 Automator 通过简单的“运行 AppleScript”服务来执行此操作。
当我右键单击 ics 文件并激活该服务时,它 运行 没有任何更改。当我从 Automator 运行 它只是 returns false,我认为这意味着它无法写入文件。
on run {inputfile}
set filedata to inputfile as string
set filedata to replaceText("Title 1", "Title 2", filedata)
writeTextToFile(filedata, inputfile, true)
end run
on replaceText(find, replace, textString)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set textString to text items of textString
set AppleScript's text item delimiters to replace
set textString to "" & textString
set AppleScript's text item delimiters to prevTIDs
return textString
end replaceText
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
问题出在第2行吗?我认为您不需要指定文件路径,因为服务应该正在输入文件。
使用 macOS High Sierra 并将 Automator Service 设置为 在 [Finder] 和单个 运行 AppleScript action 中接收选定的 [文件或文件夹],以下 example AppleScript code 为我修改 ICS 文件:
- 请注意,按照编码,它可以处理多个选定的文件,并且只能修改有效的 ICS files。
on run {input, parameters}
repeat with anItem in input
set thisFile to anItem
if (get {folder, kind} of (info for thisFile)) is {false, "ICS file"} then
set fileData to (read thisFile)
set newFileData to replaceText("Title 1", "Title 2", fileData)
writeTextToFile(newFileData, thisFile, true)
end if
end repeat
end run
on replaceText(find, replace, textString)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to find
set textString to text items of textString
set AppleScript's text item delimiters to replace
set textString to "" & textString
set AppleScript's text item delimiters to prevTIDs
return textString
end replaceText
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile