
後方ペースト
両方ダウンロードはこちら
https://note.com/quicktimer/n/nb2342201a861
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 003 | (* |
| 004 | クリップボードのテキストを |
| 005 | CotEditorに列ペースト |
| 006 | 列ペースト |
| 007 | 区切り文字 タブ |
| 008 | 挿入位置 前 |
| 009 | com.cocolog-nifty.quicktimer.icefloe *) |
| 010 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 011 | use AppleScript version "2.8" |
| 012 | use framework "Foundation" |
| 013 | use framework "AppKit" |
| 014 | use scripting additions |
| 015 | property refMe : a reference to current application |
| 016 | ############################## |
| 017 | #設定項目 区切り文字 |
| 018 | set strSeparater to ("" & tab & "") as text |
| 019 | ############################## |
| 020 | # クリップボードの中身取り出し |
| 021 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
| 022 | set ocidPastBoardTypeArray to appPasteboard's types() |
| 023 | set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSStringPboardType) |
| 024 | if (boolContain as boolean) is true then |
| 025 | set ocidClipString to appPasteboard's stringForType:(refMe's NSStringPboardType) |
| 026 | else |
| 027 | set strAlertMsg to ("クリップボードにテキストがありません") as text |
| 028 | set strButton1 to ("処理を中止します") as text |
| 029 | set listButtons to {strButton1} as list |
| 030 | try |
| 031 | tell application "SystemUIServer" |
| 032 | activate |
| 033 | set recordResponse to (display alert ("【エラー】:" & strAlertMsg) buttons listButtons default button strButton1 cancel button strButton1 as informational giving up after 10) as record |
| 034 | end tell |
| 035 | on error strErrMsg number numErrNo |
| 036 | log strErrMsg & numErrNo |
| 037 | return false |
| 038 | end try |
| 039 | return false |
| 040 | end if |
| 041 | ############################## |
| 042 | # CotEditor |
| 043 | tell application "CotEditor" |
| 044 | set numCnt to (count of every document) as integer |
| 045 | end tell |
| 046 | if numCnt = 0 then |
| 047 | return "書類がありません" |
| 048 | end if |
| 049 | tell application "CotEditor" |
| 050 | tell front document |
| 051 | properties |
| 052 | set strText to its contents |
| 053 | set strFileName to its name |
| 054 | set aliasFilePath to its file |
| 055 | #改行コード取得 |
| 056 | set valueNewLineChr to (its line ending) |
| 057 | set strEnc to its encoding |
| 058 | end tell |
| 059 | end tell |
| 060 | if aliasFilePath = (missing value) then |
| 061 | set strAlertMsg to ("前面ドキュメントが未保存です" & return & "先に保存してください") as text |
| 062 | set strButton1 to ("処理を中止します") as text |
| 063 | set listButtons to {strButton1} as list |
| 064 | try |
| 065 | tell application "SystemUIServer" |
| 066 | activate |
| 067 | set recordResponse to (display alert ("【エラー】:" & strAlertMsg) buttons listButtons default button strButton1 cancel button strButton1 as informational giving up after 10) as record |
| 068 | end tell |
| 069 | on error strErrMsg number numErrNo |
| 070 | log strErrMsg & numErrNo |
| 071 | return false |
| 072 | end try |
| 073 | end if |
| 074 | tell application "CotEditor" |
| 075 | tell front document |
| 076 | if valueNewLineChr is CRLF then |
| 077 | log "改行コードはCRLF=WINDOWSです" |
| 078 | set strNewLine to ("" & return & linefeed & "") as rich text |
| 079 | else if valueNewLineChr is CR then |
| 080 | log "改行コードはCR=MacOSです" |
| 081 | set strNewLine to ("" & return & "") as rich text |
| 082 | else if valueNewLineChr is LF then |
| 083 | log "改行コードはLF=UNIXです" |
| 084 | set strNewLine to ("" & linefeed & "") as rich text |
| 085 | end if |
| 086 | end tell |
| 087 | end tell |
| 088 | #ocidClipString |
| 089 | set ocidClipString to (ocidClipString's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(strNewLine)) |
| 090 | set ocidClipString to (ocidClipString's stringByReplacingOccurrencesOfString:(return) withString:(strNewLine)) |
| 091 | set ocidClipString to (ocidClipString's stringByReplacingOccurrencesOfString:(linefeed) withString:(strNewLine)) |
| 092 | set ocidClipArray to ocidClipString's componentsSeparatedByString:(strNewLine) |
| 093 | set numCntClipArray to ocidClipArray's |count|() |
| 094 | #CotEditor Contents |
| 095 | set ocidContentString to refMe's NSMutableString's stringWithString:(strText) |
| 096 | set ocidContentArray to ocidContentString's componentsSeparatedByString:(strNewLine) |
| 097 | set numCntContentArray to ocidContentArray's |count|() |
| 098 | # |
| 099 | if numCntClipArray > numCntContentArray then |
| 100 | set numLoopTimes to numCntClipArray |
| 101 | else if numCntClipArray < numCntContentArray then |
| 102 | set numLoopTimes to numCntContentArray |
| 103 | else if numCntClipArray = numCntContentArray then |
| 104 | set numLoopTimes to numCntContentArray |
| 105 | end if |
| 106 | |
| 107 | set ocidOutputString to refMe's NSMutableString's alloc()'s init() |
| 108 | |
| 109 | repeat with itemNo from 0 to (numLoopTimes - 1) by 1 |
| 110 | #前半 |
| 111 | if itemNo ≤ (numCntClipArray - 1) then |
| 112 | set ocidClipLine to (ocidClipArray's objectAtIndex:(itemNo)) |
| 113 | (ocidOutputString's appendString:(ocidClipLine)) |
| 114 | else |
| 115 | (ocidOutputString's appendString:("")) |
| 116 | end if |
| 117 | |
| 118 | #区切り文字 |
| 119 | (ocidOutputString's appendString:(strSeparater)) |
| 120 | |
| 121 | #後半 |
| 122 | if itemNo ≤ (numCntContentArray - 1) then |
| 123 | set ocidContentLine to (ocidContentArray's objectAtIndex:(itemNo)) |
| 124 | (ocidOutputString's appendString:(ocidContentLine)) |
| 125 | else |
| 126 | (ocidOutputString's appendString:("")) |
| 127 | end if |
| 128 | |
| 129 | (ocidOutputString's appendString:(strNewLine)) |
| 130 | end repeat |
| 131 | log ocidOutputString as text |
| 132 | set strOutputString to ocidOutputString as text |
| 133 | #ファイル名とパス |
| 134 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 135 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 136 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 137 | set ocidFileName to ocidFilePath's lastPathComponent() |
| 138 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
| 139 | set ocidExtension to ocidFilePath's pathExtension() |
| 140 | if (ocidExtension as text) is ("") then |
| 141 | set ocidExtension to ("txt") as text |
| 142 | end if |
| 143 | set strSetFileName to ("" & ocidBaseFileName & ocidExtension & "") as text |
| 144 | |
| 145 | tell application "CotEditor" |
| 146 | #この方法は上手くいかなくなったので停止 |
| 147 | # set refActivDoc to make new document with properties {name:strSetFileName, line ending:valueNewLineChr} |
| 148 | tell front document |
| 149 | set contents to strOutputString |
| 150 | properties |
| 151 | end tell |
| 152 | end tell |
| 153 | |
| 154 | return 0 |
| AppleScriptで生成しました | |
