AppleScript CotEditor

[CotEditor]文字コード変更


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

###処理対象のファイルUTI1つ
property strBundleID : ("com.apple.applescript.text")

on run
  set appFileManager to refMe's NSFileManager's defaultManager()
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
  set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
  set strMes to ("ファイルを選んでください") as text
  set strPrompt to ("ファイルを選んでください") as text
  set listUTI to {strBundleID} as list
  try
    ### ファイル選択時
    set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles, multiple selections allowed and showing package contents) as list
  on error
    log "エラーしました"
return "エラーしました"
  end try
  if listAliasFilePath is {} then
return "選んでください"
  end if
  
open listAliasFilePath
end run


on open listAliasFilePath
  
  repeat with itemAliasPath in listAliasFilePath
    ###入力パス
    set strFilePath to (POSIX path of itemAliasPath) as text
    set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath))
    set aliasFilePath to (ocidFilePathURL's absoluteURL()) as alias
    ###フォルダ除外
    set listBool to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
    if (item 2 of listBool) = (refMe's NSNumber's numberWithBool:true) then
      log "ディレクトリは処理しない"
    end if
    ###UTI取得
    set listContentTypeKey to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
    set ocidContentType to (item 2 of listContentTypeKey)
    set strUTI to (ocidContentType's identifier) as text
    ###対象のUTIファイルタイプだけ処理する
    if strUTI is strBundleID then
      ###本処理
      tell application "CotEditor"
open file aliasFilePath
###ファイルOPENを待つ
repeat 5 times
set numCnt to (count of document) as integer
if numCnt > 0 then
exit repeat
else
delay 0.2
end if
end repeat
###開いたドキュメント
tell front document
properties
###改行をUNIX LFに
set refLineEnc to line ending
if refLineEnc is CRLF then
log "改行コードはCRLF=WINDOWSです"
set line ending to LF
else if refLineEnc is CR then
log "改行コードはCR=MacOSです"
set line ending to LF
end if
###文字コードをUTF8に
set strCharEnc to (IANA charset) as rich text
if strCharEnc is not "utf-8" then
convert to "utf-8" without lossy and BOM
end if
###上書きしてどじる
save
close
###ウィンドウが閉じるのを待つ
end tell
repeat 5 times
set numCntActvDoc to (count of every window) as integer
if numCntActvDoc = 0 then
exit repeat
else
delay 0.1
end if
###ターミナルで実行できるように755アクセス権にする
set ocidFileAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
# 777-->511 755-->493 700-->448 766-->502
(ocidFileAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions))
set listDone to (appFileManager's setAttributes:(ocidFileAttrDict) ofItemAtPath:(ocidFilePath) |error|:(specifier))
end repeat
      end tell
    else
      log "UTIが違えばは処理しない"
    end if
  end repeat
  log "処理終了"
end open


|

[CotEditor]選択範囲をHTMLに(途中)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
##############################
#### 選択範囲のテキストを取得
##############################

tell application "CotEditor"
  activate
  tell application "System Events"
    tell process "CotEditor"
      tell menu bar 1
## click menu item "すべてを選択" of menu "編集"
try
click menu item "リッチテキストとしてコピー" of menu "編集"
end try
      end tell
    end tell
  end tell
end tell
##############################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidReadRtfStrings to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeRTF)
if ocidReadRtfStrings = (missing value) then
  tell application "CotEditor"
    activate
    tell application "System Events"
      tell process "CotEditor"
tell menu bar 1
## click menu item "すべてを選択" of menu "編集"
try
click menu item "リッチテキストとしてコピー" of menu "編集"
end try
end tell
      end tell
    end tell
  end tell
  set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidReadRtfStrings to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeRTF)
end if
log className() of ocidReadRtfStrings as text
###
set ocidRftData to ocidReadRtfStrings's dataUsingEncoding:(refMe's NSUTF8StringEncoding)
###アトリビュートレコード
set ocidAttarDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set ocidKey to (refMe's NSDocumentTypeDocumentAttribute)
set ocidValue to (refMe's NSRTFTextDocumentType)
ocidAttarDict's setObject:(ocidValue) forKey:(ocidKey)

set listRtf2AttrString to refMe's NSAttributedString's alloc()'s initWithData:(ocidRftData) options:(ocidAttarDict) documentAttributes:(missing value) |error|:(reference)
set ocidRtfData to (item 1 of listRtf2AttrString)



###アトリビュートレコード
set ocidAttarDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
set ocidKey to (refMe's NSDocumentTypeDocumentAttribute)
set ocidValue to (refMe's NSHTMLTextDocumentType)
ocidAttarDict's setObject:(ocidValue) forKey:(ocidKey)
###除外リスト
set ocidKey to (refMe's NSExcludedElementsDocumentAttribute)
set ocidValue to {"doctype", "xml", "html", "body", "meta", "head", "font"} as list
ocidAttarDict's setObject:(ocidValue) forKey:(ocidKey)
###文字数数えて
set ocidLength to ocidRtfData's |length|()
set ocidDataRange to refMe's NSMakeRange(0, ocidLength)
###HTMLに
set listHTMLData to ocidRtfData's dataFromRange:(ocidDataRange) documentAttributes:(ocidAttarDict) |error|:(reference)
set ocidHTMLData to (item 1 of listHTMLData)
-->ConcreteMutableData
set ocidHTMLString to refMe's NSString's alloc()'s initWithData:(ocidHTMLData) encoding:(refMe's NSUTF8StringEncoding)
set ocidHtmlStringM to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidHtmlStringM's setString:(ocidHTMLString)

######ここから置換 ここはお好みで
set listRgex to refMe's NSRegularExpression's regularExpressionWithPattern:"margin:[^;]+;\\s*" options:(refMe's NSRegularExpressionCaseInsensitive) |error|:(reference)
set ocidRgex to (item 1 of listRgex)
set ocidLength to ocidHtmlStringM's |length|()
set ocidRange to refMe's NSMakeRange(0, ocidLength)
ocidRgex's replaceMatchesInString:(ocidHtmlStringM) options:0 range:(ocidRange) withTemplate:"margin:1px;"

######ここから置換 ここはお好みで
set listRgex to refMe's NSRegularExpression's regularExpressionWithPattern:"font:[^;]+;\\s*" options:(refMe's NSRegularExpressionCaseInsensitive) |error|:(reference)
set ocidRgex to (item 1 of listRgex)
set ocidLength to ocidHtmlStringM's |length|()
set ocidRange to refMe's NSMakeRange(0, ocidLength)
ocidRgex's replaceMatchesInString:(ocidHtmlStringM) options:0 range:(ocidRange) withTemplate:""


set strStartTag to ("<div class=\"CotEditor\" style=\"background-color: #373837;font-family: Osaka-Mono,Menlo, Monaco, 'Courier New', monospace, Menlo, Monaco, 'Courier New', monospace;font-weight: normal;font-size: 16px;line-height: 17px;padding: 5px;\">") as text
set strEndTag to ("</div>")
set strOutPutText to (strStartTag & ocidHtmlStringM & strEndTag) as text


##set strText to listUTl as text
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
#####ダイアログに戻す
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" as alias
###ダイアログ
set recordResult to (display dialog "type identifier 戻り値です" with title "uniform type identifier" default answer strOutPutText buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" giving up after 20 with icon aliasIconPath without hidden answer)
###クリップボードコピー
if button returned of recordResult is "クリップボードにコピー" then
  set strText to text returned of recordResult as text
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
end if



|

[CotEditor]日付挿入


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application


##############################
#### 選択範囲のテキストを取得
##############################
tell application "CotEditor"
  set numCntActvDoc to (count of document) as integer
  if numCntActvDoc = 0 then
return "ドキュメントがありません"
  end if
  tell front document
    set strSelectionContents to contents of selection
  end tell
end tell
set strChkText to strSelectionContents as text

##ダイアログ用のArray
set ocidArrayM to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
###今日
set ocidDate to refMe's NSDate's |date|()
####################################
###フォーマット初期化日本語
####################################
set ocidFormatterJP to refMe's NSDateFormatter's alloc()'s init()
set ocidCalendarJP to refMe's NSCalendar's alloc()'s initWithCalendarIdentifier:(refMe's NSCalendarIdentifierJapanese)
set ocidTimezoneJP to refMe's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo")
set ocidLocaleJP to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP_POSIX")
###フォーマットをセット
ocidFormatterJP's setTimeZone:(ocidTimezoneJP)
ocidFormatterJP's setLocale:(ocidLocaleJP)
ocidFormatterJP's setCalendar:(ocidCalendarJP)
ocidFormatterJP's setDateFormat:("Gyy")
###今日の日付にフォーマットを適応
set ocidDateStringEra to ocidFormatterJP's stringFromDate:(ocidDate)
ocidFormatterJP's setDateFormat:("年MM月dd日")
set ocidDateString to ocidFormatterJP's stringFromDate:(ocidDate)
ocidArrayM's addObject:((ocidDateStringEra as text) & (ocidDateString as text))
###フォーマットをセット
ocidFormatterJP's setTimeZone:(ocidTimezoneJP)
ocidFormatterJP's setLocale:(ocidLocaleJP)
ocidFormatterJP's setCalendar:(ocidCalendarJP)
ocidFormatterJP's setDateFormat:("Gyy")
###今日の日付にフォーマットを適応
set ocidDateStringEra to ocidFormatterJP's stringFromDate:(ocidDate)
ocidFormatterJP's setDateFormat:("年MM月dd日EEEE")
set ocidDateString to ocidFormatterJP's stringFromDate:(ocidDate)
ocidArrayM's addObject:((ocidDateStringEra as text) & (ocidDateString as text))
####################################
###フォーマット初期化通常
####################################
set ocidFormatter to refMe's NSDateFormatter's alloc()'s init()
ocidFormatter's setTimeStyle:(refMe's NSDateFormatterNoStyle)
##
ocidFormatter's setDateStyle:(refMe's NSDateFormatterLongStyle)
set ocidDateString to ocidFormatter's stringFromDate:(ocidDate)
ocidArrayM's addObject:(ocidDateString)
##
ocidFormatter's setDateStyle:(refMe's NSDateFormatterShortStyle)
set ocidDateString to ocidFormatter's stringFromDate:(ocidDate)
ocidArrayM's addObject:(ocidDateString)
##
ocidFormatter's setDateFormat:("yyyyMMdd")
set ocidDateString to ocidFormatter's stringFromDate:(ocidDate)
ocidArrayM's addObject:(ocidDateString)
##
ocidFormatter's setDateFormat:("yyyy年MM月dd日EEEE")
set ocidDateString to ocidFormatter's stringFromDate:(ocidDate)
ocidArrayM's addObject:(ocidDateString)
###########ダイアログ用にリストに
set listDate to ocidArrayM as list
###ダイアログを前面に出す
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if
try
  set listResponse to (choose from list listDate with title "選んでください" with prompt "クリップボードにコピーします" default items (item 1 of listDate) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
  log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしました"
else
  set strText to (item 1 of listResponse) as text
end if


############################
####
############################

tell application "CotEditor"
  tell front document
    properties
    set contents of selection to strChkText & strText
  end tell
end tell




|

[CotEditor]HTMLタグ除去

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

tell application "CotEditor"
  tell front document
    replace for "<([^>]+)>" to "" with all, ignore case and RE without backwards and wrap
  end tell
end tell

tell application "CotEditor"
  tell front document
    ####改行コード取得
    set valueNewLineChr to (line ending)
  end tell
  if valueNewLineChr is CRLF then
    replace for "\r\n\r\n" to "\r\n" with all, ignore case and RE without backwards and wrap
    log "改行コードはCRLFWINDOWSです"
  else if valueNewLineChr is CR then
    replace for "\r\r" to "\r" with all, ignore case and RE without backwards and wrap
    log "改行コードはCRMacOSです"
  else if valueNewLineChr is LF then
    replace for "<([^>]+)>" to "\n" with all, ignore case and RE without backwards and wrap
    replace for "\n\n" to "\n" with all, ignore case and RE without backwards and wrap
    log "改行コードはLFUNIXです"
  end if
end tell

|

[CotEditor]タブ区切りテキストをHTMLのテーブルに変換(途中)

Applescriptでの処理は、考えすぎ…ヤリすぎは禁物です。
ある程度『現状』にあった方法でやっておいて
後でが良いようだ



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

tell application "CotEditor"
    tell front document
        set strText to contents
        set strActivDocName to name
        ####改行コード取得
        set valueNewLineChr to (line ending)
    end tell
    if valueNewLineChr is CRLF then
        log "改行コードはCRLF=WINDOWSです"
    else if valueNewLineChr is CR then
        log "改行コードはCR=MacOSです"
    else if valueNewLineChr is LF then
        log "改行コードはLF=UNIXです"
    end if
end tell

set strText to strText as text

set ocidText to refMe's NSString's stringWithString:strText
set ocidText to ocidText's stringByTrimmingCharactersInSet:(refMe's NSCharacterSet's whitespaceAndNewlineCharacterSet)
############################
######改行毎でリスト化
############################
######改行コード
set boolCRLF to ocidText's containsString:"\r\n"
set boolCR to ocidText's containsString:"\r"
#####格納用リストの初期化
set ocidTextArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
#####改行コード判定
if boolCRLF is true then
    log "改行コードはCRLF=WINDOWSです"
    set ocidTextArray to ocidText's componentsSeparatedByString:"\r\n"
else if boolCR is true then
    log "改行コードはCR=MacOSです"
    set ocidLineEndChrSet to refMe's NSCharacterSet's characterSetWithCharactersInString:"\r"
    set ocidTextArray to ocidText's componentsSeparatedByCharactersInSet:ocidLineEndChrSet
else
    set boolLF to ocidText's containsString:"\n"
    if boolLF is true then
        log "改行コードはLF=UNIXです"
        set ocidLineEndChrSet to refMe's NSCharacterSet's characterSetWithCharactersInString:"\n"
        set ocidTextArray to ocidText's componentsSeparatedByCharactersInSet:ocidLineEndChrSet
    else
        log "改行の無い1行の文字列です"
    end if
end if
############################
######必要な事項
############################
####行数
set numRowLine to (count of ocidTextArray) as integer

####最大列数
set numMaxColumnLine to 0 as integer
repeat with itemTextArray in ocidTextArray
    set ocidCntTabArray to (itemTextArray's componentsSeparatedByString:"\t")
    set numCntTab to (count of ocidCntTabArray) as integer
    if numMaxColumnLine < numCntTab then
        set numMaxColumnLine to numCntTab as integer
    end if
end repeat
log "行数:" & numRowLine
log "最大列数:" & numMaxColumnLine

############################
######本処理の準備
############################
###Tヘッドの内容を格納するようのArray
set ocidTheadArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
###CSS
set strCssText to doMakeCss()
###HTMLテーブル
set strOutPutHTML to strCssText & "<div class=\"CSS CLASS\">\n<table  class=\"CSS CLASS\">\n<caption>キャプションテキスト</caption>\n" as text
############################
######本処理
############################
##行数初期化
set numCntRowLine to 0 as integer
###行数だけ繰り返し
repeat numRowLine times
    ####行のデータを取り出し
    set ocidRowText to (ocidTextArray's objectAtIndex:numCntRowLine)
    ###タブ区切りで行データをArrayに
    set ocidRowArray to (ocidRowText's componentsSeparatedByString:"\t")
    set numCntRowArray to (count of ocidRowArray) as integer
    
    ##############################################
    ####ヘッダー行処理
    if numCntRowLine = 0 then
        ###ヘッダー行のコラム数は最大を前提で
        set strOutPutHTML to strOutPutHTML & "<thead>\n<tr>\n" as text
        ###行処理数初期化
        set numCntRowColumn to 0 as integer
        repeat numMaxColumnLine times
            set strTitle to "R" & (numCntRowLine + 1) & "C" & (numCntRowColumn + 1) as text
            if numMaxColumnLine (numCntRowColumn + 1) then
                
                if numMaxColumnLine > (numCntRowColumn + 1) then
                    set strColumnText to (ocidRowArray's objectAtIndex:(numCntRowColumn)) as text
                    set strColumnHTML to "<th title=\"" & strTitle & "\">" & strColumnText & "</th>" as text
                    set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                    
                else if numMaxColumnLine = (numCntRowColumn + 1) then
                    if numMaxColumnLine > numCntRowArray then
                        set strColumnText to "" as text
                        set strColumnHTML to "<th title=\"" & strTitle & "\" scope=\"col\">" & strColumnText & "</th>" as text
                        set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                    else if numMaxColumnLine = (numCntRowColumn + 1) then
                        set strColumnText to (ocidRowArray's objectAtIndex:(numCntRowColumn)) as text
                        set strColumnHTML to "<th title=\"" & strTitle & "\" scope=\"col\">" & strColumnText & "</th>" as text
                        set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                    else
                        set strColumnText to (ocidRowArray's objectAtIndex:(numCntRowColumn)) as text
                        set strColumnHTML to "<th title=\"" & strTitle & "\">" & strColumnText & "</th>" as text
                        set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                    end if
                    
                else
                    set strColumnText to "" as text
                    set strColumnHTML to "<th title=\"" & strTitle & "\" scope=\"col\">" & strColumnText & "</th>" as text
                    set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                end if
            else
                ###値が無い場合の空行
                set strColumnText to "" as text
                set strColumnHTML to "<th title=\"" & strTitle & "\" scope=\"col\">" & strColumnText & "</th>" as text
                set strOutPutHTML to strOutPutHTML & strColumnHTML as text
            end if
            (ocidTheadArray's addObject:strColumnText)
            set numCntRowColumn to numCntRowColumn + 1 as integer
        end repeat
        set strOutPutHTML to strOutPutHTML & "\n</tr>\n</thead>\n<tbody>\n" as text
    end if
    ##############################################
    ####ボディ行処理
    ##############################################
    ####TFOOT処理
    if (numCntRowLine + 1) = numRowLine then
        log "最後の行"
        set strOutPutHTML to strOutPutHTML & "</tbody>\n<tfoot>\n"
        #######コメント行にする場合
        ###    set strOutPutHTML to strOutPutHTML & "<tr><th colspan=\"" & numMaxColumnLine & "\">フッターテキスト</th></tr>"
        set strOutPutHTML to strOutPutHTML & "<tr>\n" as text
        set numCntRowColumn to 0 as integer
        repeat numMaxColumnLine times
            set strTitle to "R" & (numCntRowLine + 1) & "C" & (numCntRowColumn + 1) as text
            log numCntRowArray
            log numCntRowColumn
            
            set ocidHeaderText to ocidTheadArray's objectAtIndex:numCntRowColumn
            
            if numMaxColumnLine (numCntRowColumn + 1) then
                if numCntRowArray (numCntRowColumn + 1) then
                    set strColumnText to (ocidRowArray's objectAtIndex:(numCntRowColumn)) as text
                    if numCntRowColumn = 0 then
                        set strFirstRowColumn to strColumnText
                        set strColumnHTML to "<th class=\"firstcolumn\" title=\"" & strTitle & ":" & strFirstRowColumn & ":" & ocidHeaderText & "\"  scope=\"row\">" & strColumnText & "</th>" as text
                    else
                        set strColumnHTML to "<td title=\"" & strTitle & ":" & strFirstRowColumn & ":" & ocidHeaderText & "\">" & strColumnText & "</td>" as text
                    end if
                    set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                else
                    ###値が無い場合の空行
                    set strColumnText to "" as text
                    set strColumnHTML to "<td title=\"" & strTitle & ":" & strFirstRowColumn & ":" & ocidHeaderText & "\">" & strColumnText & "</td>" as text
                    set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                end if
            else
                ###値が無い場合の空行
                set strColumnText to "" as text
                set strColumnHTML to "<td title=\"" & strTitle & "\">" & strColumnText & "</td>" as text
                set strOutPutHTML to strOutPutHTML & strColumnHTML as text
            end if
            set numCntRowColumn to numCntRowColumn + 1 as integer
        end repeat
        set strOutPutHTML to strOutPutHTML & "\n</tr>\n" as text
        
        set strOutPutHTML to strOutPutHTML & "</tfoot>\n"
        ##############################################
        ####通常行
    else if numCntRowLine ≠ 0 then
        
        set strOutPutHTML to strOutPutHTML & "<tr>\n" as text
        set numCntRowColumn to 0 as integer
        repeat numMaxColumnLine times
            set strTitle to "R" & (numCntRowLine + 1) & "C" & (numCntRowColumn + 1) as text
            log numCntRowArray
            log numCntRowColumn
            
            set ocidHeaderText to ocidTheadArray's objectAtIndex:numCntRowColumn
            
            if numMaxColumnLine (numCntRowColumn + 1) then
                if numCntRowArray (numCntRowColumn + 1) then
                    set strColumnText to (ocidRowArray's objectAtIndex:(numCntRowColumn)) as text
                    if numCntRowColumn = 0 then
                        set strFirstRowColumn to strColumnText
                        set strColumnHTML to "<th class=\"firstcolumn\" title=\"" & strTitle & ":" & strFirstRowColumn & ":" & ocidHeaderText & "\"  scope=\"row\">" & strColumnText & "</th>" as text
                    else
                        set strColumnHTML to "<td title=\"" & strTitle & ":" & strFirstRowColumn & ":" & ocidHeaderText & "\">" & strColumnText & "</td>" as text
                    end if
                    set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                else
                    ###値が無い場合の空行
                    set strColumnText to "" as text
                    set strColumnHTML to "<td title=\"" & strTitle & ":" & strFirstRowColumn & ":" & ocidHeaderText & "\">" & strColumnText & "</td>" as text
                    set strOutPutHTML to strOutPutHTML & strColumnHTML as text
                end if
            else
                ###値が無い場合の空行
                set strColumnText to "" as text
                set strColumnHTML to "<td title=\"" & strTitle & "\">" & strColumnText & "</td>" as text
                set strOutPutHTML to strOutPutHTML & strColumnHTML as text
            end if
            set numCntRowColumn to numCntRowColumn + 1 as integer
        end repeat
        set strOutPutHTML to strOutPutHTML & "\n</tr>\n" as text
    end if
    
    
    set numCntRowLine to numCntRowLine + 1 as integer
end repeat

log strOutPutHTML
############
#####TFOOT処理Tfootをコメントや備考欄に使う時用
###コメント用set strOutPutHTML to strOutPutHTML & "</tbody>\n<tfoot><tr><th colspan=\"" & numMaxColumnLine & "\">フッターテキスト</th></tr></tfoot>\n</table>\n</div>"

set strOutPutHTML to strOutPutHTML & "</table>\n</div>"

tell application "CotEditor"
    set strHTMLName to (strActivDocName & ".html") as rich text
    make new document with properties {name:strHTMLName, line ending:valueNewLineChr}
    tell front document
        activate
        set contents to strOutPutHTML
    end tell
end tell


to doMakeCss()
    set strCssText to "<style>"
    set strCssText to strCssText & "table { border-collapse:  collapse;} th,td {border: solid 1px; }th{font-weight: bold;}"
    set strCssText to strCssText & "</style>"
    return strCssText
end doMakeCss


|

[CotEditor]前面書類がHTMLならSafariで開く

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

#                       com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application


####設定項目

##UTI

set strUTI to "com.apple.Safari" as text

##指定拡張子

set strExtension to "html" as text


(*

主要なUTI

"com.apple.Safari"

"com.google.Chrome"

"com.microsoft.edgemac"

"org.mozilla.firefox"

"com.operasoftware.Opera"

"com.vivaldi.Vivaldi"

"com.brave.Browser"

"org.chromium.Chromium"

*)


tell application "CotEditor"

    set numCntActvDoc to (count of document) as integer

    if numCntActvDoc = 0 then

        return "ドキュメントがありません"

    end if

    tell front document

        log "処理開始"

    end tell

    set aliasFilePath to (file of front document) as alias

end tell

set strFilePath to POSIX path of aliasFilePath as text


#####パスのString

set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)

#####パスにして

set ocidFilePath to ocidFilePathStr's stringByStandardizingPath

###URLに

set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath


#####拡張子を取得

set ocidExtensionName to ocidFilePath's pathExtension()

#####小文字にして

set ocidExtensionNameLC to ocidExtensionName's lowercaseString()

####テキストで確定しておく

set strExtensionName to ocidExtensionNameLC as text


####拡張子がHTMLなら

if strExtensionName is strExtension then

    ####UTIからアプリケーションのパスを求める

    set ocidWorkspace to refMe's NSWorkspace's sharedWorkspace()

    set ocidAppPathURL to ocidWorkspace's URLForApplicationWithBundleIdentifier:strUTI

    

    if ocidAppPathURL is (missing value) then

        tell application "Finder"

            ###UTIからアプリケーションのパスを取得

            set fileAppPath to application file id strUTI

            ###エリアス

            set aliasAppPath to fileAppPath as alias

            ###UNIXパス

            set strAppPath to POSIX path of aliasAppPath as text

            set ocidAppBundlePathStr to (refMe's NSString's stringWithString:strAppBundlePath)

            set ocidAppBundlePath to ocidAppBundlePathStr's stringByStandardizingPath

            ####アプリケーションのパスをURLに

            set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidAppBundlePath

        end tell

    end if

    

    ########処理分岐(スクリプトメニュー対策)

    tell current application

        set strName to name as text

    end tell

    if strName is "osascript" then

        set aliasAppPathURL to ocidAppPathURL as alias

        set aliasFilePathURL to ocidFilePathURL as alias

        tell application "Finder"

            open file aliasFilePathURL using aliasAppPathURL

        end tell

        return "Finderで起動しました"

    else

        

        ###開くURLを格納用のArrayを作成して

        set ocidOpenURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0

        ###開くURLをArrayに格納

        ocidOpenURLArray's addObject:ocidFilePathURL

        

        ####NSWorkspaceの初期化

        set objNSWorkspace to refMe's NSWorkspace's sharedWorkspace()

        ####コンフィグレーション

        set ocidOpenConfiguration to refMe's NSWorkspaceOpenConfiguration's configuration()

        ocidOpenConfiguration's setHides:(false as boolean)

        ocidOpenConfiguration's setRequiresUniversalLinks:(false as boolean)

        ####開く

        objNSWorkspace's openURLs:ocidOpenURLArray withApplicationAtURL:ocidAppPathURL configuration:ocidOpenConfiguration completionHandler:(missing value)

        #####

        return "NSWorkspaceで起動しました"

        

    end if

end if


|

[CotEditor]日付挿入

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

# com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application

property refNSDate : a reference to refMe's NSDate

property refNSCalendar : a reference to refMe's NSCalendar

property refNSTimeZone : a reference to refMe's NSTimeZone

property refNSDateFormatter : a reference to refMe's NSDateFormatter


###日付フォーマットは<http://nsdateformatter.com>参照

set listDateFormat to {"AD yyyyMMdd", "AD yyyy年MM月dd日", "AD yyyy年MM月dd日EEEE", "AD Gyyyy年MM月dd日EEEE", "JE yyMMdd", "JE Gyyyy年MM月dd日", "JE Gyyyy年MM月dd日 EEEE", "MMM.dd,yyyy", "EEEE MMMM dd yyyy"} as list


##############################

#### 選択範囲のテキストを取得

##############################

tell application "CotEditor"

  activate

  set numCntActvDoc to (count of document) as integer

  if numCntActvDoc = 0 then

    return "ドキュメントがありません"

  end if

  tell front document

    set strSelectionContents to contents of selection

  end tell

end tell

set strChkText to strSelectionContents as text


############################

####ダイアログを出す

############################

try

  tell current application

    activate

    set listResponse to (choose from list listDateFormat with title "選んでください" with prompt "日付フォーマット" default items (item 1 of listDateFormat) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list

  end tell

on error

  log "エラーしました"

  return "エラーしました"

  error "エラーしました" number -200

end try


if (item 1 of listResponse) is false then

  return "キャンセルしました"

  error "キャンセルしました" number -200

end if

############################

####戻り値が複数選択の本処理

############################

###ペーストボードに戻すテキストの初期化

set strOutPutText to "" as text

###繰り返し

repeat with itemResponse in listResponse

  ####テキストで確定

  set strResponse to itemResponse as text

  ####戻り値の最初がADから始まる場合は

  if strResponse starts with "AD" then

    set strDateFormat to doReplace(strResponse, "AD ", "")

    ####英語モード書式での日付を取得

    set strDateText to doGetDateNo(strDateFormat) as text

  else if strResponse starts with "JE" then

    ####戻り値の最初がJEから始まる場合は

    set strDateFormat to doReplace(strResponse, "JE ", "")

    ####日本語モード書式での日付を取得

    set strDateText to doGetDateNoJP(strDateFormat) as text

  else

    ###無印は英語モード

    set strDateText to doGetDateNo(strResponse) as text

  end if

  ####戻り値

  set strOutPutText to strDateText & "" as text

end repeat

############################

####日本語書式

############################


if strChkText is "" then

  tell application "CotEditor"

    tell front document

      properties

      set contents of selection to strOutPutText

    end tell

  end tell

else

  tell application "CotEditor"

    tell front document

      properties

      set contents of selection to strOutPutText & strSelectionContents

    end tell

  end tell

end if



############################

####日本語書式

############################

to doGetDateNoJP(strDateFormat)

  ####日付情報の取得

  set ocidDate to refNSDate's |date|()

  ###日付のフォーマットを定義

  set ocidNSDateFormatter to refNSDateFormatter's alloc()'s init()

  set ocidJPCalendar to refNSCalendar's alloc()'s initWithCalendarIdentifier:(refMe's NSCalendarIdentifierJapanese)

  set ocidNStimezoneJP to refNSTimeZone's alloc()'s initWithName:"Asia/Tokyo"

  ocidNSDateFormatter's setTimeZone:ocidNStimezoneJP

  ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP")

  ocidNSDateFormatter's setCalendar:ocidJPCalendar

  ocidNSDateFormatter's setDateFormat:strDateFormat

  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate

  set strDateAndTime to ocidDateAndTime as text

  return strDateAndTime

end doGetDateNoJP


############################

####英語書式

############################

to doGetDateNo(strDateFormat)

  ####日付情報の取得

  set ocidDate to refNSDate's |date|()

  ###日付のフォーマットを定義

  set ocidNSDateFormatter to refNSDateFormatter's alloc()'s init()

  ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"en_US")

  ocidNSDateFormatter's setDateFormat:strDateFormat

  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate

  set strDateAndTime to ocidDateAndTime as text

  return strDateAndTime

end doGetDateNo


############################

####文字の置換

############################

to doReplace(argOrignalText, argSearchText, argReplaceText)

  set ocidOrignalText to refMe's NSString's stringWithString:argOrignalText

  set ocidReplasetText to ocidOrignalText's stringByReplacingOccurrencesOfString:argSearchText withString:argReplaceText

  set strReplasetText to ocidReplasetText as text

  return strReplasetText

end doReplace

|

[CotEditor]前面書類がHTMLならブラウザを指定して開く

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

#                       com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application


####設定項目

##UTI

set strUTI to "com.apple.Safari" as text

###APP名

set strAppName to "Safari" as text


#############################

####前面書類のファイルパスを取得

#############################

tell application "CotEditor"

  set numCntActvDoc to (count of document) as integer

  if numCntActvDoc = 0 then

    return "ドキュメントがありません"

  end if

  tell front document

    log "処理開始"

  end tell

  set aliasFilePath to (file of front document) as alias

end tell

set strFilePath to POSIX path of aliasFilePath as text


#####パスのString

set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)

#####パスにして

set ocidFilePath to ocidFilePathStr's stringByStandardizingPath

###URLに

set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath


#####拡張子を取得

set ocidExtensionName to ocidFilePath's pathExtension()

#####小文字にして

set ocidExtensionNameLC to ocidExtensionName's lowercaseString()

####テキストで確定しておく

set strExtensionName to ocidExtensionNameLC as text


#############################

####本処理

#############################

####拡張子がHTMLなら

if strExtensionName is "html" then

  ####UTIからアプリケーションのパスを求める

  set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:strUTI

  if ocidAppBundle is (missing value) then

    ####この方法は予備

    set aliasAppPath to path to application strAppName as alias

    set strAppBundlePath to POSIX path of aliasAppPath as text

    set ocidAppBundlePath to (refMe's NSString's stringWithString:strAppBundlePath)

  else

    set ocidAppBundlePath to ocidAppBundle's bundlePath()

  end if

  ####アプリケーションのパスをURLに

  set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidAppBundlePath

  log ocidAppPathURL's |path| as text

  

  ###開くURLを格納用のArrayを作成して

  set ocidOpenURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0

  ###開くURLをArrayに格納

  ocidOpenURLArray's addObject:ocidFilePathURL

  

  ####NSWorkspaceの初期化

  set objNSWorkspace to refMe's NSWorkspace's sharedWorkspace()

  ####コンフィグレーション

  set ocidOpenConfiguration to refMe's NSWorkspaceOpenConfiguration's configuration()

  ocidOpenConfiguration's setHides:(false as boolean)

  ocidOpenConfiguration's setRequiresUniversalLinks:(false as boolean)

  ####開く

  objNSWorkspace's openURLs:ocidOpenURLArray withApplicationAtURL:ocidAppPathURL configuration:ocidOpenConfiguration completionHandler:(missing value)

  

end if


|

[CotEditor]%エンコードをデコード エンコード

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#URLエンコードされている%エンコード文字列を読める形にデコード
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.6"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString


##############################
#### 選択範囲のテキストを取得
##############################
tell application "CotEditor"
tell front document
set strInputText to contents of selection
end tell
end tell
##############################
#### デコード
##############################
set strOutPutText to doPercentDecode(strInputText) as text
##############################
#### 戻り値テキストで置き換え
##############################
tell application "CotEditor"
tell front document
set contents of selection to strOutPutText
end tell
end tell


on doPercentDecode(argInputText)
set ocidRawUrl to refNSString's stringWithString:argInputText
set ocidEncodedURL to ocidRawUrl's stringByRemovingPercentEncoding
set strDecodedURL to ocidEncodedURL as text
return strDecodedURL
end doPercentDecode

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#%エンコード
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.6"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString


##############################
#### 選択範囲のテキストを取得
##############################
tell application "CotEditor"
tell front document
set strInputText to contents of selection
end tell
end tell
##############################
#### デコード
##############################
set strOutPutText to doPercentEncode(strInputText) as text
##############################
#### 戻り値テキストで置き換え
##############################
tell application "CotEditor"
tell front document
set contents of selection to strOutPutText
end tell
end tell

##############################
#### エンコード サブ
##############################
on doPercentEncode(argInputText)
set ocidRawUrl to refNSString's stringWithString:argInputText
set ocidQueryAllowChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet()
set ocidEncodedURL to ocidRawUrl's stringByAddingPercentEncodingWithAllowedCharacters:ocidQueryAllowChrSet
set strDecodedURL to ocidEncodedURL as text
return strDecodedURL
end doPercentEncode

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom