Saat mengedit teks dalam seleksi menggunakan Applescript di layanan Automator, apakah mungkin untuk menjaga lekukan asli sambil menambahkan spasi tab di depannya?
Alur Kerja dan Skrip Automator
on run {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
Cuplikan berikut menunjukkan hasil yang diharapkan dan output saat ini menggunakan alur kerja dan skrip yang disebutkan di atas:
Input Sampel
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
Hasil yang diharapkan
/*
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
*/
Output Saat Ini
/*
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
*/
run
handler memiliki if
pernyataan dengan else
cabang , sehingga Anda perlu untuk menunjukkan sampel input dan output yang diharapkan sampel untuk kedua cabang , bukan hanya else
cabang .
if
pernyataan memeriksa untuk melihat apakah teks sudah diformat, dan jika demikian maka kembali ke keadaan semula, yaitu uncommented