Al editar el texto de la selección utilizando Applescript en un servicio de Automator, ¿es posible mantener la sangría original mientras se agregan espacios con pestañas?
Flujo de trabajo y secuencia de comandos de 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
El siguiente fragmento de código muestra los resultados esperados y la salida actual utilizando el flujo de trabajo y el script mencionados anteriormente:
Entrada de muestra
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
Resultado esperado
/*
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
*/
Salida actual
/*
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
*/