Tenemos un equipo de subtítulos que trabaja para varias plataformas de streaming y algunas de ellas utilizan sistemas antiguos que no aceptan algunos caracteres como , ", ", etc. Así que construí un script para reemplazar esos caracteres e informar de algunos que podrían necesitar más atención. El problema es que el código está colocando una nueva línea en cada ocurrencia y no puedo encontrar la razón. No sé cómo desgranar script, pero si es más fácil aceptaré cualquier sugerencia.
Código:
on run
set theFlags to "Characters " -- prepares te variable to store weird characters
tell application "TextEdit" to activate
delay 0.1
tell application "System Events"
key code 0 using command down -- command + A (select all)
delay 0.1
key code 8 using command down -- command + C (copy to clipboard)
delay 0.2
set theText to the clipboard
delay 0.5
-- REPLACE KNOWN CHARACTERS
set theText to my replace_chars(theText, "", "") --(0x200b) zero width space
delay 0.5
set theText to my replace_chars(theText, "…", "...")
delay 0.5
set theText to my replace_chars(theText, "“", "\"")
delay 0.5
set theText to my replace_chars(theText, "”", "\"")
delay 0.5
set theText to my replace_chars(theText, "’", "'")
delay 0.5
set theText to my replace_chars(theText, "‘", "'")
delay 0.5
-- FLAGS STRANGE CHARACTERS
if (theText contains "ª") then
set theFlags to theFlags & "ª "
end if
if (theText contains "º") then
set theFlags to theFlags & "º "
end if
if (theText contains "") then
set theFlags to theFlags & " "
end if
if (theText contains "¨") then
set theFlags to theFlags & "¨ "
end if
-- REPORT DETECTED STRANGE CHARACTERS
if (theFlags is not "Characters ") then
display dialog theFlags & " detected." & return & "Inform localization team to correct."
end if
delay 0.1
set the clipboard to theText
delay 0.5
end tell
tell application "TextEdit" to activate
delay 0.2
tell application "System Events"
key code 9 using command down -- command + V (replace old text)
end tell
end run
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
Estado inicial:
Estado final: