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

タグ: Word

新刊電子書籍「AppleScriptでたのしむ レトロ・グラフィック プログラム集」を刊行

Posted on 8月 31 by Takaaki Naganoya

電子書籍新刊「AppleScriptでたのしむ レトロ・グラフィック プログラム集」を刊行しました。全154ページ、サンプルAppleScriptアーカイブつき。
→ 販売ページ

1980年代や90年代の8/16ビットPCのBASICで描かせていた、三角関数による各種グラフィックスをAppleScriptで再現。ダイアログで表示するだけでなく、各種GUIアプリ(Keynote、Numbers、Pages、Excel、PowerPoint、Word、Pixelmator Pro)を操作して描画したり、画像書き出ししてAirDropでiOSデバイスに転送するようなサンプルを収録しています。

目次

1章 レトロ・グラフィックスの世界

懐かしのレトロCGの世界を再現
時代を経て感じる郷愁とも異なるテイスト
その昔、十数分かけて描いた三角関数グラフ
1秒以下で終了 vs 6分で終了
最新環境で動くAppleScriptにBASICのプログラムを移植
アップルスクリプトは、構文色分け必須の、色で要素を見分ける環境
最低限の知識でAppleScriptによるグラフィックを
AppleScript書類内に、実行に必要なライブラリを同梱
筆者の関数計算ライブラリ「calcLibAS」内蔵関数
コラム ポケコンエミュレータ“pockemul”

2章 早足で紹介するAppleScriptの世界

1994年から採用され続けている言語
GUIアプリを操作するために存在。搭載実行環境がとても多い
書き方は、アプリ内に存在する用語辞書を参照
本来の機能を利用するためにはシステム設定で許可する必要が
10.10以降でCocoaを直接呼べるようになったインタプリタ言語
GUI部品を直接操作してアプリを操作する強制操作機能が人気?
Web上のAPIを呼んでクラウド系の機能も利用
AS自体で予約語と機能を記述するライブラリ機能
コラム AppleScriptの世界の全体像 OS機能の最深部からGUIそのものの操作まで

3章 AppleScriptでグラフィックスを扱う

Cocoaの機能を呼び出してメモリ上で画像を作成
NSAlertの上にNSImageViewを作成しグラフィック表示
Cocoaのグラフィックス座標系”
主要なアプリケーションの座標系”
画像ファイルに書き出せば”
他のアプリにコピー&ペースト”
当時は存在していなかった透過画像”
パラメータを変えると動作が変わる”
コラム GUIアプリごとの応答速度の違い

4章 レトロ・グラフィックスプログラム集

スクリプトエディタでオープンして実行するだけ
必要なライブラリはバンドル内にすべて格納
掲載リストはグラフィックス描画にかかわる箇所のみ
How to use/ダイアログ表示AppleScript
How to use/ファイル出力AppleScript
How to use/ファイル出力+AirDrop AppleScript
How to use/クリップボード転送AppleScript
How to use/各種GUIアプリ操作AppleScript
OS標準搭載の13の実行環境およびサードパーティの数十の実行環境

線画テスト
円画テスト①
円画テスト②
サイクロイド曲線
バラ曲線
パスカルの蝸牛形
リサージュ曲線
ダイヤモンドパターン
アルキメデスの螺旋
メキシカンハット①
メキシカンハット②
メキシカンハット③
メキシカンハット④
コラム マシンごとの実行速度の違い

Posted in Books news | Tagged 13.0savvy 14.0savvy 15.0savvy Excel Keynote Numbers Pages Pixelmator Pro PowerPoint Word | Leave a comment

指定のWordファイルをPDFに書き出す

Posted on 5月 4 by Takaaki Naganoya

ほとんど書き捨てレベルのAppleScriptですが、Microsoft Office用のAppleScriptの動作状況(ランタイム環境を変更すると動かなくなるなど)に興味があったので、書いてみました。

初回実行時にWord書類が入っているフォルダへのアクセス許可を求めるダイアログが出ますが、2回目以降はとくに聞かれません。

また、PowerPoint用のAppleScriptで、スクリプトメニューから実行すると実行がブロックされるという「Microsoft、正気か?」という仕様になっていましたが、いまMicrosoft Word用の本AppleScriptをスクリプトメニューに入れて呼び出しても実行できることを確認しています(macOS 13.6.7+Word バージョン 16.84 (24041420))。

AppleScript名:指定のWordファイルをPDFに書き出す.scpt
set aFile to choose file
automatorRun({aFile}, missing value) of me

on automatorRun(input, parameters)
  repeat with i in input
    set ipath to i as text
    
    
tell application "System Events"
      set o to name of i
    end tell
    
    
set o to repChars(ipath, ".docx", ".pdf") of me
    
    
tell application "Microsoft Word"
      close every document without saving
      
open i
      
save as document 1 file name o file format (format PDF)
      
close every document without saving
    end tell
  end repeat
  
  
return ""
end automatorRun

on repChars(origText as string, targStr as string, repStr as string)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChars

★Click Here to Open This Script 

本Scriptを書くことになったreddit上の投稿への回答となるScriptはこちら。

AppleScript名:指定のWordファイルをPDFに書き出す(For Automator Action).scpt

on run {input, parameters}
  repeat with i in input
    set ipath to i as text
    
    
tell application "System Events"
      set o to name of i
    end tell
    
    
set o to repChars(ipath, ".docx", ".pdf") of me
    
    
tell application "Microsoft Word"
      close every document without saving
      
open i
      
save as document 1 file name o file format (format PDF)
      
close every document without saving
    end tell
  end repeat
  
  
return ""
end run

on repChars(origText as string, targStr as string, repStr as string)
  set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
  
set temp to text items of origText
  
set AppleScript’s text item delimiters to repStr
  
set res to temp as text
  
set AppleScript’s text item delimiters to txdl
  
return res
end repChars

★Click Here to Open This Script 

Posted in Object control PDF | Tagged 12.0savvy 13.0savvy 14.0savvy Word | Leave a comment

指定フォルダ以下のWordのファイルをすべてPDFに書き出す

Posted on 6月 22, 2019 by Takaaki Naganoya

指定フォルダ以下のWordファイルをSpotlight機能でリストアップし、かたっぱしからPDF書き出しするAppleScriptです。

Wordの制御以外はありもののルーチンを組み合わせただけなので、とくにたいした手間がかかっているわけではありません。

逆に、Wordでファイルをオープンするという動作が割とたいへんだということがわかりました。Wordの書類によっては変換の必要があるようで、オープン時に形式を聞かれました。Word書類を作成したWordのバージョン次第のようですが、注意したいところです。Script中ではオートで変換してくれるように指定しておいたものの、それほど現場でさまざまな書類を処理したわけではありません。

処理する書類の古さや、作成したWordがどのOS上で作られたかによっては、さらに何かを追加指定する必要があるかもしれません(確認は作成時最新のWord v16.24+macOS 10.12.6にて行いました)。

Spotlightでサーチして、指定フォルダ以下のWord書類をすべてリストアップしたあと、オープンしてPDFに書き出してクローズ、という動作を繰り返します。実にAppleScript「らしい」処理です。

これを、GUI Scripting(メニューやボタンの強制的な操作)だけで行うサンプルもあるようですが、GUI Scriptingは各アプリケーションの持っているAppleScriptのコマンド(AppleScript用語辞書に書かれています)に記載のない命令を「仕方なく」「例外的に」実行するだけのものであって、こういう「命令が最初から用意されているコマンド」を実行させてもメリットが何もありません。

GUI Scriptingを使ったScriptを評して「遅い」とか「確実性がない」と言われることが多いようですが、それはGUI Scriptingを使っているからです。GUI Scriptingを使うと、AppleScript本来の処理速度よりも10〜100倍ぐらい遅くなる(Cocoaの機能を活用している現代のAppleScriptだとそこから50倍ぐらい高速)ものなので、GUI Scriptingが(System Eventsを用いてメニューやボタンのクリックが行われている)多用されているAppleScriptは、本来の性能を活かせない可能性が高い(もっと高速に処理できる余地がある)ことを知っていただきたいところです。

本Scriptはそのような意味をこめて作成・公開したものです。

–> Download WordFileToPDFExporter Run-Only (Applet)

AppleScript名:WordFileToPDFExporter.scptd
— Created 2019-06-20 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use mdLib : script "Metadata Lib" version "2.0.0" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#Metadata_Lib

property NSString : a reference to current application’s NSString
property NSFileManager : a reference to current application’s NSFileManager
property NSOrderedSame : a reference to current application’s NSOrderedSame

set basePath to (path to desktop) as string –Target Folder to write PDFs
set origPath to POSIX path of (choose folder with prompt "Select Word File Orig Folder") –(path to documents folder)

–Find Word (.doc & .docx ) files by Spotlight
set resList to mdLib’s searchFolders:{origPath} searchString:("kMDItemContentTypeTree == %@ || kMDItemContentTypeTree == %@") searchArgs:{"org.openxmlformats.wordprocessingml.document", "com.microsoft.word.doc"}

if resList = {} then
  –No Word Files or Spotlight index is broken
  
display dialog "There is no Word File within your folder.." buttons {"OK"} default button 1 with icon 1
  
return
end if

set resNumLen to length of ((length of resList) as string)

–Close Word Documents without saving
closeEveryWordDocs() of me

set sCount to 1 –saving file counter
set erCount to 0

–Loop by Word files
repeat with i in resList
  set j to contents of i
  
set anAlias to (POSIX file j) as alias
  
set dRes to openWordFile(anAlias) of me
  
  
if dRes is not equal to false then
    set numStr to retZeroPaddingText(sCount, resNumLen + 1) of me
    
set newFile to basePath & numStr & "_" & (repFileNameExtension(dRes, ".pdf") of me)
    
saveWordDocAsPDF(newFile) of me
  else
    set erCount to erCount + 1
  end if
  
  
set sCount to sCount + 1 –Increment
end repeat

–Report Finish
if erCount = 0 then
  activate
  
display dialog (sCount as string) & " Word files are converted to PDF.." buttons {"OK"} default button 1 with icon 1
else
  if erCount = 1 then
    set pluralStr to ""
  else
    set pluralStr to "s"
  end if
  
  
activate
  
display dialog (sCount as string) & " Word files are converted to PDF.." & return & "But " & (erCount as string) & "file" & pluralStr & " caused error to open" buttons {"OK"} default button 1 with icon 1
end if

–最前面のWord Documentを指定パスにPDFとして書き出す
on saveWordDocAsPDF(saveHFSpathStr)
  tell application "Microsoft Word"
    save as active document file name saveHFSpathStr file format format PDF
    
close front document saving no
  end tell
end saveWordDocAsPDF

–指定パスのWord書類をオープンする
on openWordFile(aFileAlias)
  tell application "Microsoft Word"
    try
      –このあたり、予想外にいろいろオープン処理を止めるダイアログ表示などがあるようなので試行錯誤が必要
      
open aFileAlias file converter open format auto
      
tell front document
        set dName to name
      end tell
    on error
      return false
    end try
  end tell
  
return dName
end openWordFile

–Word書類をすべてクローズ。未保存のものも破棄してクローズ
on closeEveryWordDocs()
  try
    tell application "Microsoft Word"
      close every document without saving
    end tell
  end try
end closeEveryWordDocs

–ファイル名から拡張子を置換する
on repFileNameExtension(origName, newExt)
  set aName to current application’s NSString’s stringWithString:origName
  
set theExtension to aName’s pathExtension()
  
if (theExtension as string) is not equal to "" then
    set thePathNoExt to aName’s stringByDeletingPathExtension()
    
set newName to (thePathNoExt’s stringByAppendingString:newExt)
  else
    set newName to (aName’s stringByAppendingString:newExt)
  end if
  
  
return newName as string
end repFileNameExtension

–数値にゼロパディングしたテキストを返す
on retZeroPaddingText(aNum, aLen)
  set tText to ("0000000000" & aNum as text)
  
set tCount to length of tText
  
set resText to text (tCount – aLen + 1) thru tCount of tText
  
return resText
end retZeroPaddingText

★Click Here to Open This Script 

Posted in file PDF | Tagged 10.12savvy 10.13savvy 10.14savvy NSFileManager NSOrderedSame NSString Word | Leave a comment

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

Google Search

Popular posts

  • macOS 13.6.5 AS系のバグ、一切直らず
  • Apple純正マウス、キーボードのバッテリー残量取得
  • PowerPoint書類の各スライドのタイトルを取得
  • CotEditorで2つの書類の行単位での差分検出
  • 指定画像をbase64エンコード文字列に変換→デコード
  • macOS 14の変更がmacOS 13にも反映
  • Cocoa-AppleScript Appletランタイムが動かない?
  • Finder上で選択中のPDFのページ数を加算
  • ディスプレイをスリープ状態にして処理続行
  • 与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3(ベンチマーク用)
  • macOS 13 TTS環境の変化について
  • 初心者がつまづきやすい「log」コマンド
  • Adobe AcrobatをAppleScriptから操作してPDF圧縮
  • 当分、macOS 14へのアップデートを見送ります
  • macOS 14、英語環境で12時間表記文字と時刻の間に不可視スペースを入れる仕様に
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • HammerspoonでLuaを実行
  • 新刊発売 AppleScript最新リファレンス v2.8対応
  • macOS 14, Sonoma 9月27日にリリース
  • PowerPointで最前面の書類をPDF書き出し

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (435) 11.0savvy (279) 12.0savvy (199) 13.0savvy (108) 14.0savvy (54) CotEditor (62) Finder (48) iTunes (19) Keynote (106) 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 (60) Pages (45) Safari (43) Script Editor (23) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • diff
  • 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
  • parallel processing
  • 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)
  • 未分類

アーカイブ

  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 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