[Script Editor]選択範囲をコメントにする
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # スクリプトメニュー用 |
004 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
005 | use AppleScript version "2.8" |
006 | use framework "Foundation" |
007 | use scripting additions |
008 | |
009 | #コメントの行頭接頭語 |
010 | set strPreFix to "#\t" as text |
011 | |
012 | #テキスト取得 |
013 | tell application "Script Editor" |
014 | tell front document |
015 | tell selection |
016 | set listRange to (character range) as list |
017 | set strGetText to contents as text |
018 | end tell |
019 | end tell |
020 | end tell |
021 | #エラー回避 |
022 | if strGetText is "" then |
023 | return "範囲を選択していません" |
024 | end if |
025 | #初期値 |
026 | set strOutputText to "" as text |
027 | set strLineEndChar to "\r" as text |
028 | #テキストをリストにして |
029 | set strDelim to AppleScript's text item delimiters |
030 | set AppleScript's text item delimiters to strLineEndChar |
031 | set listSelectText to every text item of strGetText |
032 | set AppleScript's text item delimiters to strDelim |
033 | #リストの数 |
034 | set numCntList to (count of listSelectText) as integer |
035 | #テキスト行だけ繰り返し |
036 | repeat with itemNo from 1 to numCntList by 1 |
037 | set strLineText to (item itemNo of listSelectText) as text |
038 | if itemNo = 1 then |
039 | #最初の行 |
040 | set strOutputText to (strPreFix & strLineText & strLineEndChar) as text |
041 | else |
042 | #それ以外の行 |
043 | set strOutputText to (strOutputText & strPreFix & strLineText & strLineEndChar) as text |
044 | end if |
045 | end repeat |
046 | |
047 | #テキストを戻す |
048 | tell application "Script Editor" |
049 | tell front document |
050 | tell selection |
051 | set contents to strOutputText as text |
052 | end tell |
053 | end tell |
054 | end tell |
AppleScriptで生成しました |
(**)を使ったコメント
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # スクリプトメニュー用 |
004 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
005 | use AppleScript version "2.8" |
006 | use framework "Foundation" |
007 | use scripting additions |
008 | |
009 | #テキスト取得 |
010 | tell application "Script Editor" |
011 | tell front document |
012 | tell selection |
013 | set listRange to (character range) as list |
014 | set strGetText to contents as text |
015 | end tell |
016 | end tell |
017 | end tell |
018 | #エラー回避 |
019 | if strGetText is "" then |
020 | return "範囲を選択していません" |
021 | end if |
022 | |
023 | #初期値 |
024 | set strOutputText to "" as text |
025 | set strLineEndChar to "\r" as text |
026 | #テキストをリストにして |
027 | set strDelim to AppleScript's text item delimiters |
028 | set AppleScript's text item delimiters to strLineEndChar |
029 | set listSelectText to every text item of strGetText |
030 | set AppleScript's text item delimiters to strDelim |
031 | |
032 | |
033 | ##コメントの始まり |
034 | copy "(*" to beginning of listSelectText |
035 | ##コメントの終わり |
036 | copy "*)" to end of listSelectText |
037 | |
038 | #リストの数 |
039 | set numCntList to (count of listSelectText) as integer |
040 | |
041 | #テキスト行だけ繰り返し |
042 | repeat with itemNo from 1 to numCntList by 1 |
043 | set strLineText to (item itemNo of listSelectText) as text |
044 | if itemNo = 1 then |
045 | #最初の行 |
046 | set strOutputText to (strLineText & strLineEndChar) as text |
047 | else if itemNo = numCntList then |
048 | #最後の行 |
049 | set strOutputText to (strOutputText & strLineText) as text |
050 | else |
051 | #それ以外の行 |
052 | set strOutputText to (strOutputText & strLineText & strLineEndChar) as text |
053 | end if |
054 | end repeat |
055 | |
056 | #テキストを戻す |
057 | tell application "Script Editor" |
058 | tell front document |
059 | tell selection |
060 | set contents to strOutputText as text |
061 | end tell |
062 | end tell |
063 | end tell |
AppleScriptで生成しました |
| 固定リンク