Automator 서비스에서 Applescript를 사용하여 선택한 텍스트를 편집 할 때 탭 공백을 추가하면서 원래 들여 쓰기를 유지할 수 있습니까?
자동화 워크 플로우 및 스크립트
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
다음 스 니펫은 위에서 언급 한 워크 플로우 및 스크립트를 사용하여 예상 결과 및 현재 출력을 보여줍니다.
샘플 입력
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
예상 결과
/*
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
*/
전류 출력
/*
let c1 = CLLocation(latitude: self.latitude, longitude: self.longitude)
let c2 = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
*/
답변
Xcode에서 주석을 토글하는 목표를 달성하기 위해 주석과 함께 업데이트 된 질문을 반영하도록 업데이트되었습니다.
Automator Service 에서 사용되는 아래에 추가로 표시 되는 다음 AppleScript 코드 예제 는 macOS Sierra 및 Xcode 8.3.3 에서 테스트되었으며 AppleScript 코드 예제 의 코딩 방식으로 선택한 코드에 주석 을 달거나 선택된 코드의 주석을 해제합니다 .
즉, 선택한 코드가 다음과 같은 경우
void myDelay(float value) {
NSDate *future = [NSDate dateWithTimeIntervalSinceNow:value];
[NSThread sleepUntilDate:future];
}
그리고 Automator Service 는 위 의 선택된 코드 에서 실행 되며 다음과 같습니다.
/*
void myDelay(float value) {
NSDate *future = [NSDate dateWithTimeIntervalSinceNow:value];
[NSThread sleepUntilDate:future];
}
*/
또는 선택한 코드가 다음과 같은 경우
/*
void myDelay(float value) {
NSDate *future = [NSDate dateWithTimeIntervalSinceNow:value];
[NSThread sleepUntilDate:future];
}
*/
그리고 Automator Service 는 위 의 선택된 코드 에서 실행 되며 다음과 같습니다.
void myDelay(float value) {
NSDate *future = [NSDate dateWithTimeIntervalSinceNow:value];
[NSThread sleepUntilDate:future];
}
예 애플 스크립트 코드 A의 사용을위한 실행 애플 스크립트 동작 에서 자동화 서비스 .
on run {input, parameters}
set selectedText to item 1 of input as text
set outputText to {}
if selectedText contains "/*" then
repeat with i from 2 to ((count paragraphs of selectedText) - 1)
if (count characters of paragraph i of selectedText as text) is 1 then
copy "" to end of outputText
else
copy (characters 2 thru -1 of paragraph i of selectedText as text) to end of outputText
end if
end repeat
else
copy "/*" to end of outputText
repeat with i from 1 to (count paragraphs of selectedText)
copy tab & paragraph i of selectedText to end of outputText
end repeat
copy "*/" to end of outputText
end if
set AppleScript's text item delimiters to {linefeed}
set outputText to outputText as text
set AppleScript's text item delimiters to {}
return outputText
end run
노트:
엑스 코드 중첩 된 블록을 지원하지 않습니다는 (8.8.3 어쨌든) 주석 코드를 일부 언급하는 경우 즉, 코드를 이 스크립트에 다음 추가 선택 코드 포함 코드 이전에이 스크립트 주석을 서비스를 실행하면 오류가 발생합니다. 다음을 변경하여이 문제를 해결할 수 있습니다.
if selectedText contains "/*" then
에:
if first paragraph of selectedText contains "/*" then
그러나 Xcode 는 중첩 된 주석 코드 를 오류가있는 것으로 표시합니다. 따라서이 스크립트의 실제 가치는이 스크립트가 원래 주석을 달고 나서 더 큰 블록으로 다시 선택되지 않는 코드 에서만 사용 하는 것입니다.
주석을 해제 할 때 코드를 당신은 항상 선택의 처음과 마지막 행은 블록 주석 문자를 포함, 선택해야한다 /*
과 */
선택에 어떤 선행 또는 후행 공백 라인.
또한 Xcode 에는 코드//
를 선택 하고을 눌러 선택한 행에 주석을 달 수있는 키보드 단축키가 내장되어 있습니다 . 동일한 키보드 단축키를 누르면 선행 문자가 제거 됩니다. 이것은이 스크립팅 된 솔루션을 사용하지 않아도되는 몇 가지 방법입니다.command///
Xcode 에는 주석을위한 타사 플러그인이 있으며 Google 검색은 유익합니다.
한 가지 예는 다음과 같습니다. Xcode 용 BlockComment
OP가 목표의 실제 세부 사항으로 업데이트되기 전에 원래 답변에 대한 편집 히스토리를 참조하십시오.
참고 : 예를 들어 애플 스크립트 코드는 단지입니다 및 사용하지 않는 오류 처리 및 여러 가지 방법 중 하나가 작업을 수행 보여만을위한 것입니다. 사용자는 항상 필요 / 원하는대로 적절한 오류 처리 를 추가 / 사용해야 합니다.