20260304

[CotEditor][applescript]列ペースト

前方ペースト
後方ペースト
両方ダウンロードはこちら
https://note.com/quicktimer/n/nb2342201a861

【スクリプトエディタで開く】 |

[CotEditor]列ペースト+タブ+前.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004クリップボードのテキストを
005CotEditorに列ペースト
006列ペースト
007区切り文字 タブ
008挿入位置 前
009com.cocolog-nifty.quicktimer.icefloe *)
010----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use scripting additions
015property refMe : a reference to current application
016##############################
017#設定項目 区切り文字
018set strSeparater to ("" & tab & "") as text
019##############################
020#   クリップボードの中身取り出し
021set appPasteboard to refMe's NSPasteboard's generalPasteboard()
022set ocidPastBoardTypeArray to appPasteboard's types()
023set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSStringPboardType)
024if (boolContain as boolean) is true then
025   set ocidClipString to appPasteboard's stringForType:(refMe's NSStringPboardType)
026else
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
040end if
041##############################
042#   CotEditor
043tell application "CotEditor"
044   set numCnt to (count of every document) as integer
045end tell
046if numCnt = 0 then
047   return "書類がありません"
048end if
049tell 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
059end tell
060if 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
073end if
074tell 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
087end tell
088#ocidClipString
089set ocidClipString to (ocidClipString's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(strNewLine))
090set ocidClipString to (ocidClipString's stringByReplacingOccurrencesOfString:(return) withString:(strNewLine))
091set ocidClipString to (ocidClipString's stringByReplacingOccurrencesOfString:(linefeed) withString:(strNewLine))
092set ocidClipArray to ocidClipString's componentsSeparatedByString:(strNewLine)
093set numCntClipArray to ocidClipArray's |count|()
094#CotEditor Contents
095set ocidContentString to refMe's NSMutableString's stringWithString:(strText)
096set ocidContentArray to ocidContentString's componentsSeparatedByString:(strNewLine)
097set numCntContentArray to ocidContentArray's |count|()
098#
099if numCntClipArray > numCntContentArray then
100   set numLoopTimes to numCntClipArray
101else if numCntClipArray < numCntContentArray then
102   set numLoopTimes to numCntContentArray
103else if numCntClipArray = numCntContentArray then
104   set numLoopTimes to numCntContentArray
105end if
106
107set ocidOutputString to refMe's NSMutableString's alloc()'s init()
108
109repeat 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))
130end repeat
131log ocidOutputString as text
132set strOutputString to ocidOutputString as text
133#ファイル名とパス
134set strFilePath to (POSIX path of aliasFilePath) as text
135set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
136set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
137set ocidFileName to ocidFilePath's lastPathComponent()
138set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
139set ocidExtension to ocidFilePath's pathExtension()
140if (ocidExtension as text) is ("") then
141   set ocidExtension to ("txt") as text
142end if
143set strSetFileName to ("" & ocidBaseFileName & ocidExtension & "") as text
144
145tell 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
152end tell
153
154return 0
AppleScriptで生成しました