Action

AS JavaScripted Action

Posted by sylumer, Last update 2 months ago - Unlisted

Steps

  • script (macOS only)

    let strAS = `
    on execute(p_strInput)
    	if p_strInput is "" then
    		set strOriginal to text returned of (display dialog "Enter string to convert" default answer "")
    	else
    		set strOriginal to p_strInput
    	end if
    		
    	return processString(strOriginal)
    end execute
    
    
    on processString(p_strInput)
    	set strConverted to listToString(stringToPhonetic(p_strInput, ""), " ")
    	-- Because we're passing in a selection, let's speak it too
    	say strConverted
    	return strConverted
    end processString
    
    on stringToPhonetic(p_strInput)
    	--Initialise alphabets and output
    	set strBase to "abcdefghijklmnopqrstuvwxyz0123456789"
    	set listPhonetic to {"alfa", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliett", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whiskey", "x-ray", "yankee", "zulu", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
    	set strOutput to {}
    	
    	set listInput to stringToList(p_strInput, "")
    	
    	--For every character in the text to be converted, check if it is in the base alphabet string.
    	repeat with i from 1 to length of listInput
    		if strBase contains item i of listInput then
    			--If it is, get the position, then get the item at the equivalent position in the NATO phonetic alphabet list
    			set intPosition to offset of (item i of listInput as string) in strBase
    			set end of strOutput to item intPosition of listPhonetic
    		else
    			--If it is not, just put the original untransformed character in
    			set end of strOutput to item i of listInput
    		end if
    		
    	end repeat
    	return strOutput
    end stringToPhonetic
    
    on stringToList(p_strInput, p_strTempDelimiter)
    	--Save and switch delimiter
    	set strOriginalDelimiter to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to p_strTempDelimiter
    	
    	--Convert string to list
    	set listOutput to (every text item of p_strInput)
    	
    	--Switch delimiter back and return new list
    	set AppleScript's text item delimiters to strOriginalDelimiter
    	return listOutput
    end stringToList
    
    on listToString(p_listInput, p_strTempDelimiter)
    	--Save and switch delimiter
    	set strOriginalDelimiter to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to p_strTempDelimiter
    	
    	--Convert list to string
    	set listOutput to p_listInput as string
    	
    	--Switch delimiter back and return new string
    	set AppleScript's text item delimiters to strOriginalDelimiter
    	return listOutput
    end listToString
    `;
    
    let asRunScript = AppleScript.create(strAS);
    
    if (asRunScript.execute("execute", [editor.getSelectedText()]))
    {
    	draft.content = draft.content + "\n" + asRunScript.lastResult;
    	draft.update();
    }
    else
    {
    	console.log(asRunScript.lastError);
    	context.fail(asRunScript.lastError);
    }
    
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.