在 AppleScript 中查找和替换
Find and Replace in AppleScript
我目前正在编写一个超级简单的脚本,我需要查找并替换变量中的文本(特别是在放置到 applescript 的项目的路径中。)这是我目前拥有的:
on open {dropped_items}
tell application "Finder" to set filePathLong to the POSIX path of dropped_items as text
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
get replaceText("/mpc/mayors1", "/ifs/disk1", filePathLong)
display dialog subject
end open
(排除不相关的代码并添加一个对话框来验证它是否有效)
那个 "on replaceText..." 块是我在 Stack Overflow 中搜索这个 post 的标题时得到的。我的问题是,当我尝试编译时,它告诉我它期望 "end" 但发现了 "on." 我假设它要我在可以 "on replaceText" 之前结束我的打开,但我没有不想那样做。关于我可以做些什么来让它工作的任何想法?对不起,如果这很简单,我是 AppleScript 的新手。
我知道我可以只删除前十二个字符,然后将“/ifs/disk1”添加到字符串的开头,但我想知道为什么这不起作用,以防再次发生这种情况。
您不能将一个处理程序放在另一个(明确的)处理程序中。还进行了一些其他更正。
on open dropped_items
repeat with anItem in dropped_items
set filePathLong to anItem's POSIX path
set mySubject to replaceText("/mpc/mayors1", "/ifs/disk1", filePathLong)
display dialog mySubject
end repeat
end open
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to subject as text
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
你可以关注这个https://github.com/abbeycode/AppleScripts/blob/master/Scripts/Libraries/Strings.applescript
on replace_text(this_text, search_string, replacement_string)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to prevTIDs
return this_text
end replace_text
AppleScript 也为此内置了处理程序。
https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateText.html
Finding and Replacing Text in a String
The handler in Listing 19-9 can be used to find and replace text in a
string. To use it, provide some source text, a string to find, and a
replacement string. This handler replaces any found instances of the
specified search string.
APPLESCRIPT
Open in Script Editor Listing 19-9AppleScript: Handler that finds and
replaces text in a string
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
Listing 19-10 shows how to call the handler in Listing 19-9.
APPLESCRIPT
Open in Script Editor Listing 19-10AppleScript: Calling a handler to
find and replace text in a string
set theText to "On Tuesday, I told you to have the report ready by next Tuesday."
set theText to findAndReplaceInText(theText, "Tuesday", "Friday")
--> Result: "On Friday, I told you to have the report ready by next Friday."
我目前正在编写一个超级简单的脚本,我需要查找并替换变量中的文本(特别是在放置到 applescript 的项目的路径中。)这是我目前拥有的:
on open {dropped_items}
tell application "Finder" to set filePathLong to the POSIX path of dropped_items as text
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
get replaceText("/mpc/mayors1", "/ifs/disk1", filePathLong)
display dialog subject
end open
(排除不相关的代码并添加一个对话框来验证它是否有效)
那个 "on replaceText..." 块是我在 Stack Overflow 中搜索这个 post 的标题时得到的。我的问题是,当我尝试编译时,它告诉我它期望 "end" 但发现了 "on." 我假设它要我在可以 "on replaceText" 之前结束我的打开,但我没有不想那样做。关于我可以做些什么来让它工作的任何想法?对不起,如果这很简单,我是 AppleScript 的新手。
我知道我可以只删除前十二个字符,然后将“/ifs/disk1”添加到字符串的开头,但我想知道为什么这不起作用,以防再次发生这种情况。
您不能将一个处理程序放在另一个(明确的)处理程序中。还进行了一些其他更正。
on open dropped_items
repeat with anItem in dropped_items
set filePathLong to anItem's POSIX path
set mySubject to replaceText("/mpc/mayors1", "/ifs/disk1", filePathLong)
display dialog mySubject
end repeat
end open
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to subject as text
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
你可以关注这个https://github.com/abbeycode/AppleScripts/blob/master/Scripts/Libraries/Strings.applescript
on replace_text(this_text, search_string, replacement_string)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to prevTIDs
return this_text
end replace_text
AppleScript 也为此内置了处理程序。 https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateText.html
Finding and Replacing Text in a String
The handler in Listing 19-9 can be used to find and replace text in a string. To use it, provide some source text, a string to find, and a replacement string. This handler replaces any found instances of the specified search string.
APPLESCRIPT
Open in Script Editor Listing 19-9AppleScript: Handler that finds and replaces text in a string
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
Listing 19-10 shows how to call the handler in Listing 19-9.
APPLESCRIPT
Open in Script Editor Listing 19-10AppleScript: Calling a handler to find and replace text in a string
set theText to "On Tuesday, I told you to have the report ready by next Tuesday."
set theText to findAndReplaceInText(theText, "Tuesday", "Friday")
--> Result: "On Friday, I told you to have the report ready by next Friday."