Quando si modifica il testo in selezione usando Applescript in un servizio di Automator, è possibile mantenere il rientro originale aggiungendo spazi a schede di fronte ad esso?
Flusso di lavoro e script di Automator
onrun{input,parameters}if"/*" is in item 1 of input then
set input to replace("/*", "", input as string)
set input to replace("*/", "", input as string)
set input to extract(2, 2, input)
set input to indent(input, false)
return input
else
set input to indent(input, true)
set input to "/*" & return & (input as string) & return & "*/"
return input as text
end if
end run
on replace(searchString, editString, inputString)
set previousDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchString
set stringItems to text items of inputString
set AppleScript's text item delimiters to editString
set outputString to stringItems as string
set AppleScript's text item delimiters to previousDelimiters
return outputString
end replace
on indent(input, incrementing)
set spacing to tab
set input to input's text items
if not incrementing then
set previousDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set output to replace(spacing, "", input as string)
set AppleScript's text item delimiters to previousDelimiters
return output
else
set previousDelimiters to AppleScript's text item delimiters
set output to item 1 of input as text
set AppleScript's text item delimiters to linefeed & spacing
set output to spacing & (every paragraph of output) as string
set AppleScript's text item delimiters to ""
return output
end if
end indent
on extract(startOffset, endOffset, input)
set firstParagraph to (first paragraph of input)
set lastParagraph to (last paragraph of input)
set firstLine to length of firstParagraph
set lastLine to length of lastParagraph
set input to text (firstLine + startOffset) thru -(lastLine + endOffset) of input
return input
end extract
Il seguente frammento mostra i risultati attesi e l'output corrente utilizzando il flusso di lavoro e lo script sopra indicati:
Input di esempio
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
Risultato atteso
/*
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
*/
Uscita corrente
/*
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
*/