Archive for the 'Jedit Omega' Category

2018/01/17 文字種別にカウントする v2.1(Jedit Ω版)

Jedit Ωの最前面の書類を文字種類別にカウントして結果を新規Jedit Ωドキュメントに出力するAppleScriptです。

jeditomega1.png

Jedit Ω内蔵Script Menuに入れて実行してみたら実行できませんでした。スクリプト書類でもスクリプトバンドルでも無理でした(ASOCの実行自体ができないわけではない)。OS側のScript Menuに入れて実行してください。

Jedit Ωでfront documentの本文テキストを取り出すのに若干苦労しました。なんでこんなことに、、、、

同じテキストをオープンしてみて、Jedit ΩとCotEditorで得られる文字数が若干違うことに気づいてしまいましたが、これっていったいなんでしょうか(^ー^;;

AppleScript名:文字種別にカウントする v2.1(JeditΩ版)
– Created 2018-1-11 by Takaaki Naganoya
– 2018 Piyomaru Software
use AppleScript version “2.5″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/5111

property NSString : a reference to current application’s NSString
property NSNumber : a reference to current application’s NSNumber
property NSDictionary : a reference to current application’s NSDictionary
property NSCountedSet : a reference to current application’s NSCountedSet
property NSMutableArray : a reference to current application’s NSMutableArray
property NSNumberFormatter : a reference to current application’s NSNumberFormatter
property NSRegularExpressionSearch : a reference to current application’s NSRegularExpressionSearch
property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp

set aRes to getEditorText() of me
if aRes = false then return

set myPath to retFrontDocsPath() of me
if myPath = missing value then return

set aRec to detectCharKindRatio(aRes) of me
set bRes to retFormattedStr(aRec) of me
makeNewDocument of me given parameter:(myPath & return & return & bRes)

on detectCharKindRatio(aStr as string)
  set theCountedSet to NSCountedSet’s alloc()’s initWithArray:(characters of aStr)
  
set theEnumerator to theCountedSet’s objectEnumerator()
  
  
set cCount to 0
  
set hCount to 0
  
set kCount to 0
  
set oCount to 0
  
set lfCount to 0
  
set scCount to 0
  
set anCount to 0
  
  
set totalC to length of aStr
  
set otherList to {}
  
  
repeat
    set aValue to theEnumerator’s nextObject()
    
if aValue is missing value then exit repeat
    
    
set aStr to aValue as string
    
set tmpCount to (theCountedSet’s countForObject:aValue)
    
    
set s1Res to chkKanji(aStr) of me
    
set s2Res to chkKatakana(aStr) of me
    
set s3Res to chkHiragana(aStr) of me
    
set s4Res to chkLineFeed(aStr) of me
    
set s5Res to chkSpecialSign(aStr) of me
    
set s6Res to chkAlphaNumeric(aStr)
    
    
if s1Res = true then
      set cCount to cCount + tmpCount
    else if s2Res = true then
      set kCount to kCount + tmpCount
    else if s3Res = true then
      set hCount to hCount + tmpCount
    else if s4Res = true then
      set lfCount to lfCount + tmpCount
    else if s5Res = true then
      set scCount to scCount + tmpCount
    else if s6Res = true then
      set anCount to anCount + tmpCount
    else
      set oCount to oCount + tmpCount
      
set the end of otherList to aStr
    end if
  end repeat
  
  
set ckRes to roundingUp((cCount / totalC) * 100, 1) of me
  
set kkRes to roundingUp((kCount / totalC) * 100, 1) of me
  
set hgRes to roundingUp((hCount / totalC) * 100, 1) of me
  
set otRes to roundingUp((oCount / totalC) * 100, 1) of me
  
set lfRes to roundingUp((lfCount / totalC) * 100, 1) of me
  
set scRes to roundingUp((scCount / totalC) * 100, 1) of me
  
set anRes to roundingUp((anCount / totalC) * 100, 1) of me
  
  
return {kanjiNum:cCount, kanjiRatio:ckRes, hiraganaNum:hCount, hiraganaRatio:hgRes, katakanaNum:kCount, katakanaRatio:kkRes, otherNum:oCount, otherRatio:otRes, otherContent:otherList, lineFeedCount:lfCount, lineFeedRatio:lfRes, specialSignNum:scCount, specialSignRatio:scRes, alphaNumericNum:anCount, alphaNumericRatio:anRes, totalCount:totalC}
end detectCharKindRatio

on chkKanji(aChar)
  return detectCharKind(aChar, “[一-龠]”) of me
end chkKanji

on chkHiragana(aChar)
  return detectCharKind(aChar, “[ぁ-ん]”) of me
end chkHiragana

on chkKatakana(aChar)
  return detectCharKind(aChar, “[ァ-ヶ]”) of me
end chkKatakana

on chkLineFeed(aChar)
  return aChar is in {string id 10, string id 13, string id 13 & string id 10}
end chkLineFeed

on chkSpecialSign(aChar)
  return aChar is in {“「”, “」”, “『”, “』”, “ー”, “―”, “〜”, “〜”, “!”, “?”, “&”, “/”, “《”, “》”, “#”, “…”, “・”, “♪”, “。”, “、”, “.”, “々”, ““”, “””, “*”, “(”, “)”, “(”, “)”, ” “, “ ”, “§”, “【”, “】”, “■”, “%”, “≒”}
end chkSpecialSign

on chkAlphaNumeric(aChar)
  return detectCharKind(aChar, “[a-zA-Z0-9a-zA-Z0-9]”) of me –半角全角英数字
end chkAlphaNumeric

on detectCharKind(aChar, aPattern)
  set aChar to NSString’s stringWithString:aChar
  
set searchStr to NSString’s stringWithString:aPattern
  
set matchRes to aChar’s rangeOfString:searchStr options:(NSRegularExpressionSearch)
  
if matchRes’s location() = (current application’s NSNotFound) or (matchRes’s location() as number) > 9.99999999E+8 then
    return false
  else
    return true
  end if
end detectCharKind

on roundingUp(aNum, aDigit as integer)
  set a to aNum as real
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp)
  
set aStr to aFormatter’s stringFromNumber:(NSNumber’s numberWithFloat:a)
  
return (aStr as text) as real
end roundingUp

on getEditorText()
  tell application id “jp.co.artman21.JeditOmega”
    if (count every document) = 0 then return false
    
tell front document
      return text of it
    end tell
  end tell
end getEditorText

–処理結果から出力データの組み立て
on retFormattedStr(aRec)
  set a to “漢字  :” & (repChar(“■”, ((kanjiRatio of aRec) div 10)) of me & “  “ & (kanjiRatio of aRec) as string) & “%” & return
  
set b to “ひらがな:” & (repChar(“■”, ((hiraganaRatio of aRec) div 10)) of me & “  “ & (hiraganaRatio of aRec) as string) & “%” & return
  
set c to “カタカナ:” & (repChar(“■”, ((katakanaRatio of aRec) div 10)) of me & “  “ & (katakanaRatio of aRec) as string) & “%” & return
  
set d to “改行文字:” & (repChar(“■”, ((lineFeedRatio of aRec) div 10)) of me & “  “ & (lineFeedRatio of aRec) as string) & “%” & return
  
set e to “特殊記号:” & (repChar(“■”, ((specialSignRatio of aRec) div 10)) of me & “  “ & (specialSignRatio of aRec) as string) & “%” & return
  
set f to “英数字 :” & (repChar(“■”, ((alphaNumericRatio of aRec) div 10)) of me & “  “ & (alphaNumericRatio of aRec) as string) & “%” & return & return
  
set g to (“総文字数:” & encodeJapaneseNumText(totalCount of aRec) & ” 文字。 400字詰め原稿用紙で約 “ & ((totalCount of aRec) div 400) as string) & ” 枚分” & return
  
set all to a & b & c & d & e & f & g
  
return all
end retFormattedStr

on repChar(aChar, aTimes)
  set outStr to “”
  
repeat aTimes times
    set outStr to outStr & aChar
  end repeat
  
return outStr
end repChar

–数字文字列を日本語数値表現文字列に変換
on encodeJapaneseNumText(aText as string)
  set dotText to “.” as string
  
set upperDigit to “”
  
set lowerDigit to “”
  
  
–小数点の処理
  
if dotText is in aText then
    set b to offset of dotText in aText
    
set upperDigit to characters 1 thru (b - 1) of aText
    
set upperDigit to upperDigit as string
    
set lowerDigit to characters b thru -1 of aText
    
set lowerDigit to lowerDigit as string
  else
    set upperDigit to aText
  end if
  
  
set scaleList3 to {“”, “万”, “億”, “兆”, “京”, “垓”, “丈”, “壌”, “溝”, “砂”, “正”, “載”, “極”, “恒河沙”, “阿僧梢”, “那由他”, “不可思議”, “無量大数”}
  
set splitDigit to 4
  
set nList to splitByDigit(upperDigit, splitDigit) of me
  
set nList to reverse of nList
  
  
set resText to “”
  
set digCount to 1
  
repeat with i in nList
    set b to (contents of i) as number
    
if b is not equal to 0 then
      set resText to (b as text) & item digCount of scaleList3 & resText
    end if
    
set digCount to digCount + 1
  end repeat
  
  
return resText & lowerDigit
  
end encodeJapaneseNumText

–指定桁数で区切る
on splitByDigit(a, splitDigit)
  set aList to characters of a
  
set aList to reverse of aList
  
  
set resList to {}
  
set tempT to “”
  
set tempC to 1
  
repeat with i in aList
    set tempT to contents of i & tempT
    
if tempC mod splitDigit = 0 then
      set resList to {tempT} & resList
      
set tempT to “”
    end if
    
set tempC to tempC + 1
  end repeat
  
  
if tempT is not equal to “” then
    set resList to {tempT} & resList
  end if
  
  
resList
  
end splitByDigit

on makeNewDocument given parameter:aStr
  tell application id “jp.co.artman21.JeditOmega”
    set aDoc to make new document with properties {text:aStr}
  end tell
end makeNewDocument

on retFrontDocsPath()
  tell application id “jp.co.artman21.JeditOmega”
    tell front document
      return path
    end tell
  end tell
end retFrontDocsPath

★Click Here to Open This Script 

2017/12/14 指定文字の花文字テキストを取得する(Retina対応)

指定文字を指定フォントの花文字を作成してCotEditor上に花文字で新規ドキュメントを作成するAppleScriptのRetina Display対応版です。

テキストエディタへの出力部分はTextEdit.app、BBEdit、TextWrangler、mi、Jedit Omega、CotEditor用のルーチンを差し替えればすぐに対応可能です。

以前に掲載したルーチンがRetina Display(144×144dpi)の環境を考慮していなかったので、対応させてみました(Retina Display搭載機をLid Closed Modeで運用して外部ディスプレイつないでいるので)。

Retina Display対応というのは、倍の解像度で文字が出力されるという話ではなく、Retina Displayの環境でも問題なく文字全体が出力されるということです。

flowtext.png

AppleScript名:指定文字の花文字テキストを取得する(Retina対応)
– Created 2017-12-12 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
–http://piyocast.com/as/archives/5028

property NSFont : a reference to current application’s NSFont
property NSUUID : a reference to current application’s NSUUID
property NSColor : a reference to current application’s NSColor
property NSArray : a reference to current application’s NSArray
property NSString : a reference to current application’s NSString
property NSImage : a reference to current application’s NSImage
property NSPredicate : a reference to current application’s NSPredicate
property NSDictionary : a reference to current application’s NSDictionary
property NSBezierPath : a reference to current application’s NSBezierPath
property NSColorSpace : a reference to current application’s NSColorSpace
property NSPNGFileType : a reference to current application’s NSPNGFileType
property NSFontManager : a reference to current application’s NSFontManager
property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep
property NSMutableDictionary : a reference to current application’s NSMutableDictionary
property NSFontAttributeName : a reference to current application’s NSFontAttributeName
property NSKernAttributeName : a reference to current application’s NSKernAttributeName
property NSMutableParagraphStyle : a reference to current application’s NSMutableParagraphStyle
property NSLigatureAttributeName : a reference to current application’s NSLigatureAttributeName
property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString
property NSUnderlineStyleAttributeName : a reference to current application’s NSUnderlineStyleAttributeName
property NSParagraphStyleAttributeName : a reference to current application’s NSParagraphStyleAttributeName
property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName

set aString to “あ”
set hanaSize to 36
set thisFont to selectAFont(aString) of me
if thisFont = false then return –Cancel

–花文字文字列を作成
set fRes to getHanamojiStr(hanaSize, thisFont, aString, 0.7, true) of me
makeNewDocument given parameter:fRes

–花文字文字列を計算して返す
on getHanamojiStr(aFontSize as real, aFontName as string, aString as string, aThread as real, incFontName as boolean)
  if length of aString is not equal to 1 then return false
  
  
–指定文字コードが指定フォント中に存在するかチェック
  
set fRes to retGlyphsInFont(aFontName, id of aString) of me
  
if fRes = false then return false
  
  
set aThreadShould to 768 * aThread
  
if (chkMultiByteChar(aString) of me) = false then
    set spaceChar to string id 12288 –全角スペース(UTF-16)
  else
    set spaceChar to string id 32 –半角スペース
  end if
  
  
set fillColor to NSColor’s whiteColor –塗り色
  
set bString to aString & ” “ –処理内容の帳尻合わせ(そのままだと右端が欠けるのでスペースを入れた)
  
set anAssrStr to makeRTFfromParameters(bString, aFontName, aFontSize, -2, (aFontSize * 1.2)) of me
  
set aSize to anAssrStr’s |size|()
  
  
if class of aSize = record then
    set attrStrWidth to width of aSize
    
set attrStrHeight to height of aSize
  else if class of aSize = list then –macOS 10.13.xのバグ回避
    copy aSize to {attrStrWidth, attrStrHeight}
  end if
  
  
set {xPos, yPos} to {0, 0}
  
  
set tmpImg1 to makeImageWithFilledColor(attrStrWidth, attrStrHeight, fillColor) of me
  
set tmpImg2 to drawAttributedStringsOnImage(tmpImg1, anAssrStr, xPos, yPos) of me
  
set aRawimg to NSBitmapImageRep’s imageRepWithData:(tmpImg2’s TIFFRepresentation())
  
  
–画像から順次指定座標の色データを拾って花文字listに反映
  
set strList to {}
  
repeat with y from 1 to attrStrHeight - 1
    
    
set strListX to {}
    
repeat with x from 0 to attrStrWidth - 1
      set tmpCol to getColorFromRawImage(aRawimg, x, y) of me
      
      
if tmpCol is not equal to false then
        if tmpCol is not equal to {255, 255, 255} then
          copy tmpCol to {tmpR, tmpG, tmpB}
          
if (tmpR + tmpG + tmpB) < aThreadShould then
            set the end of strListX to aString
          else
            set the end of strListX to spaceChar
          end if
        else
          set the end of strListX to spaceChar
        end if
      end if
      
    end repeat
    
set the end of strList to strListX
  end repeat
  
  
–2D List→Text
  
set aRes to list2dToStringByUsingDelimiters(strList, “”, return) of me
  
  
if incFontName = true then
    set fName to getDisplayedNameOfFont(aFontName) of me
    
set aRes to “■” & fName & return & return & aRes
  end if
  
  
return aRes
end getHanamojiStr

–指定Raw画像中の指定座標のピクセルの色をRGBで取り出す
on getColorFromRawImage(aRawimg, x as real, y as real)
  set aRatio to getImageRatio() of me –Retina Display対応
  
set origColor to (aRawimg’s colorAtX:(x * aRatio) y:(y * aRatio))
  
  
set srgbColSpace to NSColorSpace’s deviceRGBColorSpace
  
if srgbColSpace = missing value then return false
  
  
set aColor to (origColor’s colorUsingColorSpace:srgbColSpace)
  
  
set aRed to (aColor’s redComponent()) * 255
  
set aGreen to (aColor’s greenComponent()) * 255
  
set aBlue to (aColor’s blueComponent()) * 255
  
  
return {aRed as integer, aGreen as integer, aBlue as integer}
end getColorFromRawImage

–画像のうえに指定のスタイル付きテキストを描画して画像を返す
on drawAttributedStringsOnImage(anImage, anAssrStr, xPos as real, yPos as real)
  anImage’s lockFocus()
  
anAssrStr’s drawAtPoint:(current application’s NSMakePoint(xPos, yPos))
  
anImage’s unlockFocus()
  
return anImage
end drawAttributedStringsOnImage

–書式つきテキストを組み立てる
on makeRTFfromParameters(aStr as string, fontName as string, aFontSize as real, aKerning as real, aLineSpacing as real)
  set aVal1 to NSFont’s fontWithName:fontName |size|:aFontSize
  
set aKey1 to (NSFontAttributeName)
  
  
set aVal2 to NSColor’s blackColor()
  
set aKey2 to (NSForegroundColorAttributeName)
  
  
set aVal3 to aKerning
  
set akey3 to (NSKernAttributeName)
  
  
set aVal4 to 0
  
set akey4 to (NSUnderlineStyleAttributeName)
  
  
set aVal5 to 2 –all ligature ON
  
set akey5 to (NSLigatureAttributeName)
  
  
set aParagraphStyle to NSMutableParagraphStyle’s alloc()’s init()
  
aParagraphStyle’s setMinimumLineHeight:(aLineSpacing)
  
aParagraphStyle’s setMaximumLineHeight:(aLineSpacing)
  
set akey7 to (NSParagraphStyleAttributeName)
  
  
set keyList to {aKey1, aKey2, akey3, akey4, akey5, akey7}
  
set valList to {aVal1, aVal2, aVal3, aVal4, aVal5, aParagraphStyle}
  
set attrsDictionary to NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList
  
  
set attrStr to NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary
  
return attrStr
end makeRTFfromParameters

–指定サイズの画像を作成し、背景を指定色で塗る
on makeImageWithFilledColor(aWidth as real, aHeight as real, fillColor)
  set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight))
  
set aRatio to getImageRatio() of me –Retina Display対応
  
  
anImage’s lockFocus()
  
set theRect to {{x:0, y:0}, {width:aWidth * aRatio, height:aHeight * aRatio}}
  
set theNSBezierPath to NSBezierPath’s bezierPath
  
theNSBezierPath’s appendBezierPathWithRect:theRect
  
fillColor’s |set|()
  
theNSBezierPath’s fill()
  
anImage’s unlockFocus()
  
  
return anImage
end makeImageWithFilledColor

–2D Listをアイテム間および行間のデリミタを個別に指定してテキスト変換
on list2dToStringByUsingDelimiters(aList as list, itemDelimiter, lineDelimiter)
  set outList to {}
  
repeat with i in aList
    set aStr to listToStringUsingTextItemDelimiter(i, itemDelimiter) of me
    
set the end of outList to aStr
  end repeat
  
  
set bStr to listToStringUsingTextItemDelimiter(outList, lineDelimiter) of me
  
return bStr
end list2dToStringByUsingDelimiters

on listToStringUsingTextItemDelimiter(sourceList as list, textItemDelimiter)
  set CocoaArray to NSArray’s arrayWithArray:sourceList
  
set CocoaString to CocoaArray’s componentsJoinedByString:textItemDelimiter
  
return (CocoaString as string)
end listToStringUsingTextItemDelimiter

–ユーザー環境にインストールされているすべてのフォントのPostScript名とグリフ数を返す
on getEveryFontPSNameANdGlyphsNum(aStr)
  set aFontList to NSFontManager’s sharedFontManager()’s availableFonts()
  
set thePred to NSPredicate’s predicateWithFormat:“NOT SELF BEGINSWITH ’.’”
  
set aFontList to (aFontList’s filteredArrayUsingPredicate:thePred) as list
  
  
set aList to {}
  
repeat with i in aFontList
    set aName to contents of i
    
set aNum to countNumberOfGlyphsInFont(aName) of me
    
set dName to getDisplayedNameOfFont(aName) of me
    
    
set fRes to retGlyphsInFont(aName, id of aStr) of me
    
if fRes = true then
      set the end of aList to {fontName:aName, fontNum:aNum, dispName:dName}
    end if
  end repeat
  
  
return aList
end getEveryFontPSNameANdGlyphsNum

–指定Postscript名称のフォントに定義されている文字数を数えて返す
on countNumberOfGlyphsInFont(fontName as string)
  set aFont to NSFont’s fontWithName:fontName |size|:9.0
  
if aFont = missing value then return false
  
set aProp to aFont’s numberOfGlyphs()
  
return aProp as number
end countNumberOfGlyphsInFont

–フォントのPostScript NameからDisplayed Nameを取得
on getDisplayedNameOfFont(aName as string)
  set aFont to NSFont’s fontWithName:aName |size|:9.0
  
set aDispName to (aFont’s displayName()) as string
  
return aDispName
end getDisplayedNameOfFont

–全角文字が存在するか
on chkMultiByteChar(checkString as string)
  set aStr to NSString’s stringWithString:checkString
  
set aRes to aStr’s canBeConvertedToEncoding:(current application’s NSASCIIStringEncoding)
  
return (aRes as boolean)
end chkMultiByteChar

–指定名称のフォントに指定の文字コードが含まれているかチェック
on retGlyphsInFont(fontName as string, strCode as integer)
  set aFont to NSFont’s fontWithName:fontName |size|:24.0
  
if aFont = missing value then return false
  
set aSet to aFont’s coveredCharacterSet()
  
set aRes to (aSet’s characterIsMember:strCode) as boolean
  
return aRes as list of string or string –as anything
end retGlyphsInFont

on selectAFont(aString)
  set fRes to getEveryFontPSNameANdGlyphsNum(aString) of me
  
set theArray to NSArray’s arrayWithArray:fRes
  
set thePred to NSPredicate’s predicateWithFormat:“fontNum > 10000″
  
set bArray to ((theArray’s filteredArrayUsingPredicate:thePred)’s valueForKeyPath:“dispName”) as list
  
set thisFont to choose from list bArray
  
return thisFont
end selectAFont

–Retina Display対応ルーチン
on getImageRatio()
  set retinaF to detectRetinaDisplay() of me
  
if retinaF = true then
    return 2.0 as real
  else
    return 1.0 as real
  end if
end getImageRatio

on detectRetinaDisplay()
  set dispList to current application’s NSScreen’s screens()
  
set retinaF to false
  
  
repeat with i in dispList
    set j to contents of i
    
set aDepth to j’s backingScaleFactor()
    
if aDepth > 1.0 then
      set retinaF to true
    end if
  end repeat
  
  
return retinaF
end detectRetinaDisplay

on makeNewDocument given parameter:aStr
  tell application id “com.coteditor.CotEditor”
    activate
    
set newDoc to make new document
    
tell newDoc
      set contents to aStr
    end tell
  end tell
end makeNewDocument

★Click Here to Open This Script 

2017/12/13 ハンドラの間接呼び出しのじっけん

ハンドラの間接呼び出しの実験を行うAppleScriptです。

variableeditor.gif

ハンドラの間接呼び出しは、Scripter誰もが一度は考えてみる課題ですが、自分にはどう考えてもできませんでした。もしかして、世界にひとりぐらいは実現している人間がいるかもしれませんが、とりあえず自分の結論は「無理」というものでした。

# recordを文字列に変換するのも、当初は「言語仕様的に不可能」と言われた空前絶後の超絶テクニックでしたが、いまでは割と手垢のついた「手口」に

ハンドラの存在確認が行える以上、間接的にハンドラを呼び出すことも不可能ではなさそうな雰囲気もしていますが、とりあえず行えていません。

結局、異なるAppleScript Librariesに同じ名前のハンドラを作っておき、呼び出し先のAppleScript Librariesを「代入」で切り替えることで、間接呼び出しっぽい処理を実現してみました。

scriptlibs.png

別にif文で条件分岐してもかまわなさそうな内容ですが、「やってできないことはない」という落とし所が見えました。

本Scriptでは、TextEdit.app、BBEdit、TextWranglermiJedit OmegaCotEditorという6本のテキストエディタをダイアログで選択し、当該エディタで花文字テキストの新規ドキュメントを作成しています。

–> Download This Script Bundle

AppleScript名:variableHandlerTest
– Created 2017-12-12 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use ed001 : script “textedit”
use ed002 : script “bbedit”
use ed003 : script “textwrangler”
use ed004 : script “mi”
use ed005 : script “jeditomega”
use ed006 : script “coteditor”
–http://piyocast.com/as/archives/5023

set aString to retFlowerText() of me

set appIDlist to {“com.apple.textedit”, “com.barebones.bbedit”, “com.barebones.textwrangler”, “net.mimikaki.mi”, “jp.co.artman21.JeditOmega”, “com.coteditor.CotEditor”}
set scriptObjList to {ed001, ed002, ed003, ed004, ed005, ed006}

set appNameList to {}
set appBundleIDList to {}

repeat with i in appIDlist
  set anAppID to contents of i
  
set aRes to checkAppInstallation(anAppID) of me
  
if aRes is not equal to false then
    set the end of appBundleIDList to anAppID
  end if
end repeat

set cRes to choose from list appBundleIDList
if cRes = false then return –Cancel

set appIDnum to retIndexNumInArray(appIDlist, contents of first item of cRes)
set objRef to contents of item appIDnum of scriptObjList

copy objRef to theTargetEditor –Very Important!!

tell theTargetEditor
  makeNewDocument given parameter:aString
end tell

on retFlowerText()
  return “                                            
           あああああ                            
           あああああ                            
           あああああ                            
           あああああ                            
    ああああああああああああああああああああああああああああ            
    ああああああああああああああああああああああああああああ            
    ああああああああああああああああああああああああああああ            
    ああああああああああああああああああああああああああああ            
           あああああ                            
           あああああ                            
           あああああ  ああああああ                    
           あああああああああああああああああ                
           あああああああああああああああああああ              
         あああああああああ   ああああああああああ             
        ああああああああ    ああああ  ああああああ            
       あああああああああ    ああああ   あああああ            
      あああああ ああああ   あああああ    あああああ           
     あああああ  あああああ  ああああ     あああああ           
     ああああ   あああああ あああああ     あああああ           
    あああああ   ああああああああああ      あああああ           
    ああああ     あああああああああ      あああああ           
   あああああ     ああああああああ       あああああ           
   あああああ     あああああああ        あああああ           
   あああああ     ああああああ        ああああああ           
   あああああ    ああああああ         ああああああ           
   ああああああああああああああ        あああああああ            
    ああああああああああああ       ああああああああ             
    あああああああああああ   あああああああああああああ             
     ああああああああ     あああああああああああ               
        あ         あああああああああ                 
                                            
                                            

end retFlowerText

–指定IDのアプリケーションがHDD上に存在するかを確認
on checkAppInstallation(appID)
  tell application “Finder”
    try
      set aRes to exists of application file id appID
    on error
      return false
    end try
  end tell
  
  
return true
end checkAppInstallation

–1D List中のシーケンシャルサーチ
on retIndexNumInArray(aList, aTarget)
  script obj
    property list : aList
  end script
  
  
set aCount to 1
  
set hitF to false
  
  
repeat with i in obj’s list
    set j to contents of i
    
if aTarget = j then
      return aCount
    end if
    
    
set aCount to aCount + 1
  end repeat
  
  
if hitF = false then return 0
end retIndexNumInArray

★Click Here to Open This Script 

AppleScript名:textedit
on makeNewDocument given parameter:aStr
  tell application id “com.apple.textedit”
    set aDoc to make new document
    
tell aDoc
      set text of it to aStr
    end tell
    
activate
  end tell
end makeNewDocument

★Click Here to Open This Script 

AppleScript名:bbedit
on makeNewDocument given parameter:aStr
  tell application id “com.barebones.bbedit”
    set aDoc to make new text document with properties {text:aStr}
    
activate
  end tell
end makeNewDocument

★Click Here to Open This Script 

AppleScript名:textwrangler
on makeNewDocument given parameter:aStr
  tell application id “com.barebones.textwrangler”
    set aDoc to make new text document with properties {text:aStr}
    
activate
  end tell
end makeNewDocument

★Click Here to Open This Script 

AppleScript名:mi
on makeNewDocument given parameter:aStr
  tell application id “net.mimikaki.mi”
    make new document with properties {content:aStr}
    
activate
  end tell
end makeNewDocument

★Click Here to Open This Script 

AppleScript名:jeditomega
on makeNewDocument given parameter:aStr
  tell application id “jp.co.artman21.JeditOmega”
    set aDoc to make new document with properties {text:aStr}
    
activate
  end tell
end makeNewDocument

★Click Here to Open This Script 

AppleScript名:coteditor
on makeNewDocument given parameter:aStr
  tell application id “com.coteditor.CotEditor”
    activate
    
set newDoc to make new document
    
tell newDoc
      set contents to aStr
    end tell
  end tell
end makeNewDocument

★Click Here to Open This Script