I want to replace line breaks in text, so I wrote the following code using Apple Script, why the first script doesn’t work, but the second one does. The difference between the two scripts is that the first one uses “{popclip text}”, while the second one uses the clipboard directly.
- Here’s the first script.
on replace_chars(this_text, search_string, replacement_string)
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 ""
return this_text
end replace_chars
set the popclip_text to "{popclip text}"
set the updated_text to my replace_chars(popclip_text, return, " ")
tell application "Bob"
launch
translate updated_text
end tell
- Here’s the second script.
tell application "System Events"
keystroke "c" using {command down}
delay 1
end tell
on replace_chars(this_text, search_string, replacement_string)
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 ""
return this_text
end replace_chars
set the clipboard_content to get the clipboard as text
set the updated_content to my replace_chars(clipboard_content, return, " ")
set the clipboard to updated_content
tell application "Bob"
launch
translate (the clipboard)
end tell