Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

タグ: JeditΩ

front document

Posted on 9月 11, 2018 by Takaaki Naganoya

AppleScriptに対応しているGUIアプリケーションで、複数のドキュメントを管理するもの、テキストエディタや表計算ソフトなどはwindowとdocumentというクラスが定義されています。

documentは、オープンされた順にIDが振られます。このIDは、ウィンドウの重ね合わせ順が変更されても変わりません。保存したりクローズしたりする対象はdocumentです(原則。たまに例外があります)。

windowのIDは、画面上のウィンドウの重ね合わせ順を反映したものです。最前面のウィンドウがwindow 1です。その下にあるのがwindow 2、3、4……とカウントされます。別のウィンドウを最前面に移動させると、それがwindow 1になります。

windowに対して実行できるコマンドもあれば、documentに対して実行できるコマンドもあります。そのあたり、アプリケーションごとに実装が異なるので慣れが必要です。一番このあたりでdocumentとwindowの使い分けに頭を悩まされるのはFileMakerあたりでしょうか。「それ、document(database)に対してのコマンドじゃないんだ?」という例がズラズラと。

長いあいだ、「最前面のdocumentにアクセスするのって大変だ」と感じていました。window 1にアクセスして、そのpropertyを取得し、propertyの中に対応するdocumentの情報があれば、それを取得。

そんな中、front documentという書き方を見かけ、試してみたら「最前面のdocument」にアクセスできました。AppleScript用語辞書にも載っていないのに、なぜか使える用語です。

AppleScript名:window–>document
–window 1–> document
tell application "Safari"
  tell window 1
    properties
    
–> {zoomable:true, closeable:true, zoomed:true, class:window, index:1, visible:true, name:"AppleScriptの穴 – Useful & Practical AppleScript archive", miniaturizable:true, id:204098, miniaturized:false, resizable:true, bounds:{0, 22, 1233, 1200}, current tab:tab 1 of window id 204098 of application "Safari", document:document "AppleScriptの穴 – Useful & Practical AppleScript archive" of application "Safari"}
    
    
set aDoc to document of it
  end tell
  
  
tell aDoc
    set aURL to URL
    
–> "http://piyocast.com/as/"
  end tell
end tell

★Click Here to Open This Script 

AppleScript名:front document
–front document
tell application "Safari"
  tell front document
    set aURL to URL
    
–> "http://piyocast.com/as/"
  end tell
end tell

★Click Here to Open This Script 

こんな便利な機能、いつごろ実装されたのかと疑問に思って、macOS/OS X/Mac OS Xのバージョンをさかのぼり、Classic Macエミュレータ「SheepShaver」上のClassic Mac OS J1-8.6上のAppleScript J1-1.3.7で確認したところ、期待どおりの動作を行いました。

うわ、恥ずかしい〜。ただ、front documentについて書籍などで書かれているのを見た覚えがありません。

1999年に作成されたAppleScript Language Guide 1.3.7で「front document」を検索してみたら、けっこーたくさん出てきました。

Posted in How To | Tagged 10.12savvy JeditΩ Safari | Leave a comment

JeditΩの最前面のドキュメントのプロパティ取得

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:JeditΩの最前面のドキュメントのプロパティ取得
tell application id "jp.co.artman21.JeditOmega"
  tell front document
    properties
  end tell
end tell

–> {rich text:true, line endings:LF, path:"/Users/maro/Library/Mobile Documents/iCloud~jp~co~artman21~JeditOmega/Documents/名称未設定5.rtf", selected character range:{loc:21, len:0}, encoding:missing value, class:document, selected attribute range:{loc:2, len:0}, name:"名称未設定5", default attributes:{forecolor:{0, 0, 0}, backcolor:{65535, 65535, 65535}, charfont:"Helvetica", charsize:12.0}, modified:false, selected paragraph range:{loc:1, len:1}, selected text:"", text:"This is Test String.", document type:"リッチテキスト書式(.rtf)"}

–> {rich text:false, line endings:LF, path:"/Users/maro/Library/Mobile Documents/iCloud~jp~co~artman21~JeditOmega/Documents/名称未設定5.txt", selected character range:{loc:21, len:0}, encoding:missing value, class:document, selected attribute range:{loc:2, len:0}, name:"名称未設定5.rtf", default attributes:{forecolor:{0, 0, 0}, backcolor:{65535, 65535, 65535}, charfont:"Helvetica", charsize:12.0}, modified:false, selected paragraph range:{loc:1, len:1}, selected text:"", text:"This is Test String.", document type:"プレーンテキスト書類"}

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy JeditΩ | Leave a comment

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

Posted on 2月 6, 2018 by Takaaki Naganoya
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 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy JeditΩ | Leave a comment

JeditΩで新規ドキュメント作成

Posted on 2月 6, 2018 by Takaaki Naganoya
AppleScript名:JeditΩで新規ドキュメント作成
set aCon to "This is Test String."
set newDoc to makeNewDocumentWithJeditOmega(aCon, false) of me –make new plain text
–makeADocToPlan(newDoc) of me

on makeNewDocumentWithJeditOmega(aCon)
  tell application id "jp.co.artman21.JeditOmega"
    set aDoc to make new document with properties {text:aCon}
    
activate
    
return aDoc
  end tell
end makeNewDocumentWithJeditOmega

on makeADocToPlan(aDocRef)
  tell application id "jp.co.artman21.JeditOmega"
    tell aDocRef
      set rich text to false
    end tell
  end tell
end makeADocToPlan

★Click Here to Open This Script 

Posted in 未分類 | Tagged 10.11savvy 10.12savvy 10.13savvy JeditΩ | Leave a comment

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • macOS 13 TTS Voice環境に変更
  • UI Browserがgithub上でソース公開され、オープンソースに
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた
  • 従来と異なるmacOS 13の性格?
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • macOS 13でNSNotFoundバグふたたび
  • ChatGPTでchatに対する応答文を取得
  • 新発売:iWork Scripting Book with AppleScript
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)
  • AppleScriptの数値変数で指数表示にならない最大値、最小値
  • バカスタム App選手権 入賞!

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (277) 12.0savvy (186) 13.0savvy (59) CotEditor (60) Finder (47) iTunes (19) Keynote (99) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (57) Pages (38) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC