Applescript replace characters without adding new line

We have a subtitle team working for several streaming platforms and some of them use old systems that will not accept a few characters like …, “, ”, etc. So I built a script for replacing those characters and inform some that might need further attention. Problem is, the code is placing a new line at every occurrence and I can't find the reason. I don't know how to shell script, but if it is easier I will accept any suggestion.

Code:

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

Inicial state:

enter image description here

Final state:

enter image description here

Answers 1

  • Starting with:

    enter image description here

    Using the following example AppleScript code:

    tell application "TextEdit"
        tell front document
            set theText to its text
            set theTextProperties to ¬
                the properties of its text
        end tell
    end tell
    
    set theText to my replaceChars(theText, "?", "")
    set theText to my replaceChars(theText, "…", "...")
    set theText to my replaceChars(theText, "“", "\"")
    set theText to my replaceChars(theText, "”", "\"")
    set theText to my replaceChars(theText, "’", "'")
    set theText to my replaceChars(theText, "‘", "'")
    
    tell application "TextEdit"
        activate
        tell front document to ¬
            set its text to theText
    end tell
    
    delay 0.1
    
    tell application "System Events" to ¬
        key code 0 using command down
    
    tell application "TextEdit"
        tell front document
            set its font to font of theTextProperties
            set its size to size of theTextProperties
            set its color to color of theTextProperties
        end tell
    end tell
    
    delay 0.1
    
    tell application "System Events" to key code 125
    
    set theFlags to "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
    if theFlags is not "Characters " then
        activate
        display alert theFlags & " detected." & ¬
            linefeed & linefeed & ¬
            "Inform localization team to correct."
    end if
    
    on replaceChars(thisText, searchString, replacementString)
        set AppleScript's text item delimiters to the searchString
        set the itemList to text items of thisText
        set AppleScript's text item delimiters to the replacementString
        set thisText to the itemList as string
        set AppleScript's text item delimiters to ""
        return thisText
    end replaceChars
    

    Result:

    enter image description here


    Notes:

    The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

    • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

    • Recoded to avoid using the clipboard.
    • Restructured code to remove improperly nested statements.
    • Removed unnecessary parentheses.
    • Removed unnecessary delay commands.
    • Converted mismatched naming style of variables to camelCase.
    • Move test for and notification of "strange characters" to the end of the process.
    • Changed display dialog to display alert.
    • Reworked the message use with display alert. Displays as, e.g.,:

    enter image description here



    Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.


Related Questions