AppleScript iWork

[ドロップレット]iWorkとMicrosoft Officeファイルから画像を取り出してファイルにする(画像をオフィスファイルから抽出)



ダウンロード - office2imageexport4drop.zip




AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# com.cocolog-nifty.quicktimer.icefloe
004# 書き出しから アプリケーションでドロップレットになります
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use framework "UniformTypeIdentifiers"
010use scripting additions
011
012property refMe : a reference to current application
013###################
014#設定項目 対象のUTI
015property listUTI : {"org.openxmlformats.presentationml.presentation", "org.openxmlformats.spreadsheetml.sheet", "org.openxmlformats.wordprocessingml.document", "com.apple.iwork.keynote.sffkey", "com.apple.iwork.numbers.sffnumbers", "com.apple.iwork.pages.sffpages"} as list
016#
017property strUTI : ("public.image") as text
018
019
020###################
021#Wクリックで実行
022on run
023  ###ダイアログを前面に出す
024  tell current application
025    set strName to name as text
026  end tell
027  if strName is "osascript" then
028    tell application "Finder" to activate
029  else
030    tell current application to activate
031  end if
032  #ダイアログ
033  set appFileManager to refMe's NSFileManager's defaultManager()
034  set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
035  set ocidUserDesktopPath to ocidUserDesktopPathArray's firstObject()
036  #
037  set strPromptText to "Microsoft Officeファイルを選んでください" as text
038  set strMesText to "Microsoft Officeファイルを選んでください" as text
039  set listChooseAliasFilePath to (choose file strMesText with prompt strPromptText default location (ocidUserDesktopPath as alias) of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
040  #サブルーチンに渡す
041  set boolDone to doAction(listChooseAliasFilePath)
042  #戻り値がエラーだったか?
043  if boolDone is false then
044    display alert "エラーが発生しました" message "エラーが発生しました"
045    return
046  end if
047  return "処理終了RUN"
048end run
049
050###################
051#ドロップ
052on open listDropAliasFilePath
053  #サブルーチンに渡すリスト
054  set listAliasFilePath to {} as list
055  #ドロップされたアイテムの数だけ繰り返す
056  repeat with itemDropAliasFilePath in listDropAliasFilePath
057    #エイリアス
058    set aliasItemFilePath to itemDropAliasFilePath as alias
059    tell application "Finder"
060      #Finder情報を取得して
061      set recordInfoFor to info for aliasItemFilePath
062    end tell
063    #UTIを取得
064    set strItemUIT to (type identifier of recordInfoFor) as text
065    #UTIが対象ファイルならリストに追加
066    if listUTI contains strItemUIT then
067      copy aliasItemFilePath to end of listAliasFilePath
068    end if
069  end repeat
070  set numCntAliasList to (count of listAliasFilePath) as integer
071  if numCntAliasList > 0 then
072    #サブルーチンに渡す
073    set boolDone to doAction(listAliasFilePath)
074  else
075    display alert "エラーが発生しました対象のファイルではありません"
076    return "エラー終了open"
077  end if
078  #戻り値がエラーだったか?
079  if boolDone is false then
080    display alert "エラーが発生しました" message "エラーが発生しました"
081    return "エラー終了open"
082  end if
083  return "処理終了open"
084  
085end open
086
087###################
088#実行されるのはこれ
089to doAction(argListAliasFilePath)
090  ######
091  set appFileManager to refMe's NSFileManager's defaultManager()
092  #テンポラリ
093  set ocidTempDirURL to appFileManager's temporaryDirectory()
094  #フォルダ作成時の属性用
095  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
096  #ファイルエイリアスリストを順番の処理
097  repeat with itemAliasFilePath in argListAliasFilePath
098    set aliasFilePath to itemAliasFilePath as alias
099    set strFilePath to (POSIX path of aliasFilePath) as text
100    set ocidFilePath to (refMe's NSString's stringWithString:strFilePath)
101    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath))
102    #テンポラリ
103    set ocidUUID to refMe's NSUUID's alloc()'s init()
104    set ocidUUIDString to ocidUUID's UUIDString
105    set ocidUnzipDirPathURL to (ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true)
106    #テンポラリフォルダ生成 アクセス権777
107    (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions))
108    set listDone to (appFileManager's createDirectoryAtURL:(ocidUnzipDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
109    if (item 2 of listDone) ≠ (missing value) then
110      set strErrorNO to (item 2 of listDone)'s code() as text
111      set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
112      refMe's NSLog("■:" & strErrorNO & strErrorMes)
113      log "移動:エラーしました" & strErrorNO & strErrorMes
114      refMe's NSLog("■■■: 移動でエラーになりました")
115      return false
116    end if
117    set strUnzipDirPath to (ocidUnzipDirPathURL's |path|()) as text
118    ########
119    #解凍
120    set strCommandText to ("/bin/zsh -c '/usr/bin/unzip  \"" & strFilePath & "\" -d \"" & strUnzipDirPath & "\"'") as text
121    log strCommandText
122    try
123      do shell script strCommandText
124    on error
125      refMe's NSLog("■■■: コマンドでエラーになりました")
126      return false
127    end try
128    ########
129    #ファイル名
130    set ocidFileName to ocidFilePathURL's lastPathComponent()
131    #ベースファイル名
132    set ocidBaseFileName to (ocidFileName's stringByAppendingPathExtension:("export-image"))
133    #コンテナ
134    set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
135    #画像保存先
136    set ocidSaveDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(ocidBaseFileName) isDirectory:(true))
137    #フォルダ生成
138    (ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions))
139    set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
140    if (item 2 of listDone) ≠ (missing value) then
141      set strErrorNO to (item 2 of listDone)'s code() as text
142      set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
143      refMe's NSLog("■:" & strErrorNO & strErrorMes)
144      log "移動:エラーしました" & strErrorNO & strErrorMes
145    end if
146    ########
147    #コンテンツの収集
148    #プロパティ
149    set ocidPropertiesArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
150    (ocidPropertiesArray's addObject:(refMe's NSURLNameKey))
151    (ocidPropertiesArray's addObject:(refMe's NSURLPathKey))
152    (ocidPropertiesArray's addObject:(refMe's NSURLContentTypeKey))
153    #オプション
154    set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
155    #収集
156    set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidUnzipDirPathURL) includingPropertiesForKeys:(ocidPropertiesArray) options:(ocidOption) errorHandler:(reference))
157    #出力用の空の可変リスト
158    set ocidImagePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
159    ##allObjectsでループ
160    set ocidEmuFileURLArray to ocidEmuDict's allObjects()
161    #ループ
162    repeat with itemURL in ocidEmuFileURLArray
163      #コンテンツタイプを取得
164      set listResponse to (itemURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference))
165      if (item 1 of listResponse) is (true) then
166        #UTType
167        set ocidUTType to (item 2 of listResponse)
168        #UTTypeの属性全部をリストに
169        set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects()
170        #対象のUTI
171        set ocidContainUTType to (refMe's UTType's typeWithIdentifier:(strUTI))
172        #UTTypeリストに対象のUTIが含まれているか?
173        set boolImagetype to (ocidParentUTIArray's containsObject:(ocidContainUTType)) as boolean
174        if boolImagetype is (true) then
175          #出力用のリストに追加していく
176          (ocidImagePathURLAllArray's addObject:(itemURL))
177        end if
178        #エラー時はログのみ
179      else if (item 1 of listResponse) is (false) then
180        log (item 3 of listResponse)'s code() as text
181        log (item 3 of listResponse)'s localizedDescription() as text
182      end if
183    end repeat
184    ########
185    #取り出したリストの数だけ繰り返し
186    set numCntArray to (ocidImagePathURLAllArray's |count|()) as integer
187    repeat with itemIntNo from 0 to (numCntArray - 1) by 1
188      #順番にURLを取り出して
189      set ocidItemImageURL to (ocidImagePathURLAllArray's objectAtIndex:(itemIntNo))
190      #拡張子
191      set ocidSaveFileExtension to ocidItemImageURL's pathExtension()
192      #画像の読み込んで
193      set ocidImageRep to (refMe's NSImageRep's imageRepWithContentsOfURL:(ocidItemImageURL))
194      #同名回避のために連番
195      set strImageNo to (itemIntNo + 1) as text
196      if ocidImageRep ≠ (missing value) then
197        if (ocidSaveFileExtension as text) is ("svg") then
198          set strSaveFileName to ("image@" & strImageNo) as text
199        else
200          #解像度を取得
201          set strImageW to (ocidImageRep's pixelsHigh()) as text
202          set strImageH to (ocidImageRep's pixelsWide()) as text
203          #移動先ファイル名 
204          set strSaveFileName to (strImageNo & "@" & strImageW & "x" & strImageH) as text
205        end if
206      else
207        set strSaveFileName to ("image@" & strImageNo) as text
208      end if
209      #移動先のパスを作って
210      set ocidSaveFileBasePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
211      #拡張子つけて
212      set ocidSaveFilePathURL to (ocidSaveFileBasePathURL's URLByAppendingPathExtension:(ocidSaveFileExtension))
213      #移動
214      set listDone to (appFileManager's moveItemAtURL:(ocidItemImageURL) toURL:(ocidSaveFilePathURL) |error| :(reference))
215      if (item 1 of listDone) is true then
216        # log "正常処理"
217      else if (item 2 of listDone) ≠ (missing value) then
218        set strErrorNO to (item 2 of listDone)'s code() as text
219        set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
220        refMe's NSLog("■:" & strErrorNO & strErrorMes)
221        log "移動:エラーしました" & strErrorNO & strErrorMes
222      end if
223    end repeat
224    
225  end repeat
226  #全部エラーなく終わったらtrueを戻す
227  return true
228end doAction
AppleScriptで生成しました

|

iWorkとMicrosoft Officeファイルから画像を取り出してファイルにする(画像をオフィスファイルから抽出)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
005use AppleScript version "2.6"
006use framework "Foundation"
007use framework "AppKit"
008use framework "UniformTypeIdentifiers"
009use scripting additions
010
011property refMe : a reference to current application
012################################
013#設定項目 取り出すファイルUTI
014set strUTI to ("public.image") as text
015
016#ダイアログで選択できるUTI XML形式のMicrosoft Officeドキュメント
017# iworkドキュメントも同じ方法で収集できるので追加
018set listUTI to {"org.openxmlformats.presentationml.presentation", "org.openxmlformats.spreadsheetml.sheet", "org.openxmlformats.wordprocessingml.document", "com.apple.iwork.keynote.sffkey", "com.apple.iwork.numbers.sffnumbers", "com.apple.iwork.pages.sffpages"} as list
019
020################################
021set appFileManager to refMe's NSFileManager's defaultManager()
022#テンポラリ
023set ocidTempDirURL to appFileManager's temporaryDirectory()
024#フォルダ作成時の属性用
025set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
026
027###ダイアログを前面に出す
028set strName to (name of current application) as text
029if strName is "osascript" then
030  tell application "Finder" to activate
031else
032  tell current application to activate
033end if
034#
035set ocidUserDesktopPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
036set ocidUserDesktopPathURL to ocidUserDesktopPathURLArray's firstObject()
037set aliasDefaultLocation to (ocidUserDesktopPathURL's absoluteURL()) as alias
038set strPromptText to "Microsoft Officeファイルを選んでください" as text
039set strMesText to "Microsoft Officeファイルを選んでください" as text
040#ダイアログ
041set listAliasFilePath to (choose file strMesText with prompt strPromptText default location (aliasDefaultLocation) of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
042
043################################
044#本処理
045
046repeat with itemAliasFilePath in listAliasFilePath
047  #入力
048  set aliasFilePath to itemAliasFilePath as alias
049  set strFilePath to (POSIX path of aliasFilePath) as text
050  set ocidFilePath to (refMe's NSString's stringWithString:strFilePath)
051  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath))
052  #テンポラリ
053  set ocidUUID to refMe's NSUUID's alloc()'s init()
054  set ocidUUIDString to ocidUUID's UUIDString
055  set ocidUnzipDirPathURL to (ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true)
056  (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions))
057  set listDone to (appFileManager's createDirectoryAtURL:(ocidUnzipDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
058  if (item 2 of listDone) ≠ (missing value) then
059    set strErrorNO to (item 2 of listDone)'s code() as text
060    set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
061    refMe's NSLog("■:" & strErrorNO & strErrorMes)
062    log "移動:エラーしました" & strErrorNO & strErrorMes
063  end if
064  set strUnzipDirPath to (ocidUnzipDirPathURL's |path|()) as text
065  ########
066  #解凍
067  set strCommandText to ("/bin/zsh -c '/usr/bin/unzip  \"" & strFilePath & "\" -d \"" & strUnzipDirPath & "\"'") as text
068  log strCommandText
069  try
070    do shell script strCommandText
071  on error
072    display alert "解凍に失敗しました"
073    return "解凍に失敗しました"
074  end try
075  ########
076  #ファイル名
077  set ocidFileName to ocidFilePathURL's lastPathComponent()
078  #ベースファイル名
079  set ocidBaseFileName to (ocidFileName's stringByAppendingPathExtension:("export-image"))
080  #コンテナ
081  set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
082  #画像保存先
083  set ocidSaveDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(ocidBaseFileName) isDirectory:(true))
084  (*
085  よくよく考えたら不要だった
086  set ocidSaveDirPath to ocidSaveDirPathURL's |path|()
087  #フォルダ有無チェック
088  set boolDirExists to (appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true))
089  if boolDirExists = true then
090    set numCntNot to 2 as integer
091    repeat
092      set ocidSaveDirPath to (ocidSaveDirPath's stringByAppendingPathExtension:(numCntNot))
093      set boolCngDirExists to (appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true))
094      if boolDirExists = false then
095        set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:(true))
096        exit repeat
097      end if
098      set numCntNot to (numCntNot + 1) as integer
099    end repeat
100  else if boolDirExists = false then
101    log "フォルダ無いので処理継続"
102  end if
103  *)
104  #フォルダ生成
105  (ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions))
106  set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
107  if (item 2 of listDone) ≠ (missing value) then
108    set strErrorNO to (item 2 of listDone)'s code() as text
109    set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
110    refMe's NSLog("■:" & strErrorNO & strErrorMes)
111    log "移動:エラーしました" & strErrorNO & strErrorMes
112  end if
113  ########
114  #コンテンツの収集
115  #プロパティ
116  set ocidPropertiesArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
117  (ocidPropertiesArray's addObject:(refMe's NSURLNameKey))
118  (ocidPropertiesArray's addObject:(refMe's NSURLPathKey))
119  (ocidPropertiesArray's addObject:(refMe's NSURLContentTypeKey))
120  #オプション
121  set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
122  #収集
123  set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidUnzipDirPathURL) includingPropertiesForKeys:(ocidPropertiesArray) options:(ocidOption) errorHandler:(reference))
124  #出力用の空の可変リスト
125  set ocidImagePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
126  ##allObjectsでループ
127  set ocidEmuFileURLArray to ocidEmuDict's allObjects()
128  #ループ
129  repeat with itemURL in ocidEmuFileURLArray
130    #コンテンツタイプを取得
131    set listResponse to (itemURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference))
132    if (item 1 of listResponse) is (true) then
133      #UTType
134      set ocidUTType to (item 2 of listResponse)
135      #UTTypeの属性全部をリストに
136      set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects()
137      #対象のUTI
138      set ocidContainUTType to (refMe's UTType's typeWithIdentifier:(strUTI))
139      #UTTypeリストに対象のUTIが含まれているか?
140      set boolImagetype to (ocidParentUTIArray's containsObject:(ocidContainUTType)) as boolean
141      if boolImagetype is (true) then
142        #出力用のリストに追加していく
143        (ocidImagePathURLAllArray's addObject:(itemURL))
144      end if
145      #エラー時はログのみ
146    else if (item 1 of listResponse) is (false) then
147      log (item 3 of listResponse)'s code() as text
148      log (item 3 of listResponse)'s localizedDescription() as text
149    end if
150  end repeat
151  ########
152  #取り出したリストの数だけ繰り返し
153  set numCntArray to (ocidImagePathURLAllArray's |count|()) as integer
154  repeat with itemIntNo from 0 to (numCntArray - 1) by 1
155    #順番にURLを取り出して
156    set ocidItemImageURL to (ocidImagePathURLAllArray's objectAtIndex:(itemIntNo))
157    #拡張子
158    set ocidSaveFileExtension to ocidItemImageURL's pathExtension()
159    #画像の読み込んで
160    set ocidImageRep to (refMe's NSImageRep's imageRepWithContentsOfURL:(ocidItemImageURL))
161    #同名回避のために連番
162    set strImageNo to (itemIntNo + 1) as text
163    if ocidImageRep ≠ (missing value) then
164      if (ocidSaveFileExtension as text) is ("svg") then
165        set strSaveFileName to ("image@" & strImageNo) as text
166      else
167        #解像度を取得
168        set strImageW to (ocidImageRep's pixelsHigh()) as text
169        set strImageH to (ocidImageRep's pixelsWide()) as text
170        #移動先ファイル名 
171        set strSaveFileName to (strImageNo & "@" & strImageW & "x" & strImageH) as text
172      end if
173    else
174      set strSaveFileName to ("image@" & strImageNo) as text
175    end if
176    #移動先のパスを作って
177    set ocidSaveFileBasePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
178    #拡張子つけて
179    set ocidSaveFilePathURL to (ocidSaveFileBasePathURL's URLByAppendingPathExtension:(ocidSaveFileExtension))
180    #移動
181    set listDone to (appFileManager's moveItemAtURL:(ocidItemImageURL) toURL:(ocidSaveFilePathURL) |error| :(reference))
182    if (item 1 of listDone) is true then
183      # log "正常処理"
184    else if (item 2 of listDone) ≠ (missing value) then
185      set strErrorNO to (item 2 of listDone)'s code() as text
186      set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
187      refMe's NSLog("■:" & strErrorNO & strErrorMes)
188      log "移動:エラーしました" & strErrorNO & strErrorMes
189    end if
190  end repeat
191  
192end repeat
193#最後のフォルダを開く
194set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
195set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL)
196
197
198return "Done"
AppleScriptで生成しました

|

iWorkアプリのユーザーテンプレートの保存場所

20240509103119902x956
あくまでも参考にしてください

サンプルソース(参考)
行番号ソース
001Keynote
002"$HOME/Library/Containers/com.apple.iWork.Keynote/Data/Library/Application Support/User Templates"
003Numbers
004"$HOME/Library/Containers/com.apple.iWork.Numbers/Data/Library/Application Support/User Templates"
005Pages
006"$HOME/Library/Containers/com.apple.iWork.Pages/Data/Library/Application Support/User Templates"
AppleScriptで生成しました

|

Numbers全セル巡回


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

tell application "Numbers"
activate
  tell front document
    set numCntSheet to (count of every sheet) as integer
    
    repeat with itemIntNo from 1 to numCntSheet by 1
      
      tell sheet itemIntNo
        repeat with itemTable in every table
          repeat with itemRow in every row of itemTable
repeat with itemCell in every cell of itemRow
tell itemCell
properties
end tell
end repeat
          end repeat
        end repeat
      end tell
      
      
    end repeat
    
  end tell
  
end tell

|

Keynoteで差し込み印字風(実際は置換)


ダウンロード - e382abe383bce38388e38299e4bd9ce68890.zip


↑こちらをダウンロードして試してください(テンプレート入り)



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

#!/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

### 【1】入力ファイル
#ダイアログ
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
#デフォルトロケーション
tell application "Finder"
  set aliasDefaultLocation to container of (path to me) as alias
end tell
#タブ区切りテキスト(拡張子TSVなので留意してください)
set listUTI to {"public.tab-separated-values-text"}
set strMes to ("TSVファイルを選んでください") as text
set strPrompt to ("TSVファイルを選んでください") as text
try
  ### ファイル選択時
  set aliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
if aliasFilePath is (missing value) then
return "TSVファイルを選んでください"
end if
#TSVファイルパス
set strFilePath to (POSIX path of aliasFilePath) 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) isDirectory:false)
####
#テンプレート
set strTemplateFileName to ("アスクル マルチカード(名刺サイズ).key") as text
tell application "Finder"
  set aliasTemplateFilePath to file strTemplateFileName of folder "Template" of folder aliasDefaultLocation
  set aliasTemplateFilePath to aliasTemplateFilePath as alias
end tell
#テンプレートのパス
set strTemplateFilePath to (POSIX path of aliasTemplateFilePath) as text
set ocidTemplateFilePathStr to refMe's NSString's stringWithString:(strTemplateFilePath)
set ocidTemplateFilePath to ocidTemplateFilePathStr's stringByStandardizingPath()
set ocidTemplateFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidTemplateFilePath) isDirectory:false)
#保存先
set strSaveDirName to ("出来上がり") as text
set strContainerDirPath to (POSIX path of aliasDefaultLocation) as text
set strSaveDirPath to (strContainerDirPath & strSaveDirName)
set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDirPath)
set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:true)
##すでにある場合はリネームして上書きしない
set appFileManager to refMe's NSFileManager's defaultManager()
set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true)
if boolDirExists = true then
log "すでにフォルダがあります"
  #日付時間を拡張子として追加
  set strDateno to doGetDateNo("yyyyMMdd-hhmm")
  set ocidContainerDirPathStr to refMe's NSString's stringWithString:(strContainerDirPath)
  set ocidContainerDirPath to ocidContainerDirPathStr's stringByStandardizingPath()
  set ocidContainerDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidContainerDirPath) isDirectory:true)
  set ocidSaveDirExistPathURL to ocidSaveDirPathURL's URLByAppendingPathExtension:(strDateno)
  #移動
  set listDone to appFileManager's moveItemAtURL:(ocidSaveDirPathURL) toURL:(ocidSaveDirExistPathURL) |error|:(reference)
else if boolDirExists = false then
log "フォルダないので処理続行"
end if
#保存先フォルダを生成
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
##
### 【2】 ファイルのテキストを読み込み
set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
set ocidReadString to (item 1 of listResponse)
#可変にして
set ocidLineString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
(ocidLineString's setString:(ocidReadString))
#改行をUNIXに強制
set ocidLineStringLF to (ocidLineString's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n"))
set ocidLineString to (ocidLineStringLF's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
#改行毎でリストにする
set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet)
set ocidLineArray to (ocidLineString's componentsSeparatedByCharactersInSet:(ocidCharSet))
#リストの数
set numCntLine to (count of ocidLineArray) as integer
#何ページ作成されるのか?
set numPrintPage to (round of (numCntLine / 10) rounding up) as integer
#
set numCntReadLineNO to 1 as integer
repeat with itemMakePageNo from 1 to numPrintPage by 1
  set strSaveFileName to (itemMakePageNo & ".key") as text
  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName))
  #
  set appFileManager to refMe's NSFileManager's defaultManager()
  set listDone to (appFileManager's copyItemAtURL:(ocidTemplateFilePathURL) toURL:(ocidSaveFilePathURL) |error|:(reference))
  set strSaveFilePath to ocidSaveFilePathURL's |path| as text
  set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
  
  tell application "Keynote"
    set objActivDoc to open aliasSaveFilePath
    ####################################
    ###↓ここから本処理 ページ内(slide)の処理
    ###################################
    set numCntSlide to 1 as integer
    set numTempNO to 0 as integer
    repeat with itemReadLineNo from numCntReadLineNO to (numCntReadLineNO + 9) by 1
      set ocidLineText to (ocidLineArray's objectAtIndex:(itemReadLineNo))
      set ocidChrSet to (refMe's NSCharacterSet's characterSetWithCharactersInString:("\t"))
      set ocidLineTextArray to (ocidLineText's componentsSeparatedByCharactersInSet:(ocidChrSet))
      ##項目を増やしたい場合はここを調整して
      set ocidLineNo to (ocidLineTextArray's objectAtIndex:(0))
      if (ocidLineNo as text) is ("") then
        exit repeat
      end if
      set ocidName to (ocidLineTextArray's objectAtIndex:(1))
      set ocidSubject to (ocidLineTextArray's objectAtIndex:(2))
      set strLineNO to ocidLineNo as text
      set strName to ocidName as text
      set strSubject to ocidSubject as text
      set numTempNO to numTempNO + 1 as integer
      set strRepTextSub to ("肩書き" & numTempNO) as text
      set strRepTextName to ("氏名" & numTempNO) as text
      set strRepTextNo to ("No" & numTempNO) as text
      ####### Keynoteの処理
      tell objActivDoc
        tell current slide
          set listTextItem to every text item
          repeat with itemText in listTextItem
set strItemText to object text of itemText as text
if strItemText is strRepTextName then
set object text of itemText to (strName)
else if strItemText is strRepTextNo then
set object text of itemText to (strLineNO)
else if strItemText is strRepTextSub then
set object text of itemText to (strSubject)
end if
          end repeat
        end tell
      end tell
      #
      set numCntReadLineNO to numCntReadLineNO + 1 as integer
      if numCntReadLineNO is (numCntLine - 1) then
        exit repeat
      end if
    end repeat
    set numCntSlide to numCntSlide + 1 as integer
    ####################################
    ###↑ここまで本処理 ページ内(slide)の処理
    ###################################
    
    # save objActivDoc in strSaveFilePath as Keynote
    # close objActivDoc saving no
close objActivDoc saving in strSaveFilePath
    if numCntReadLineNO is (numCntLine - 1) then
      exit repeat
    end if
    #Keynoteの終わり
  end tell
end repeat

return
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL)
###結果
log boolDone
if boolDone is false then
  set aliasFilePath to (ocidSaveDirPathURL's absoluteURL()) as alias
  tell application "Finder"
open folder aliasFilePath
  end tell
return "エラーしました"
end if

return

to doGetDateNo(strDateFormat)
  ####日付情報の取得
  set ocidDate to current application's NSDate's |date|()
  ###日付のフォーマットを定義
  set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidNSDateFormatter's setDateFormat:strDateFormat
  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
  set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo


|

その他のカテゴリー

Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager Apple AppleScript AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Audio 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 Diff AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript Droplet AppleScript eMail AppleScript Encode % AppleScript Encode Decode AppleScript Encode UTF8 AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript Guide AppleScript HTML AppleScript HTML Entity AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim AppleScript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript Leading Zero AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript Mail AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Messages AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSArray Sort 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 NSFileManager enumeratorAtURL AppleScript NSFont AppleScript NSFontManager AppleScript NSGraphicsContext 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 NSpoint AppleScript NSPredicate AppleScript NSPrintOperation AppleScript NSRange AppleScript NSRect 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 NSURLSession AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotation AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFImageRep AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photoshop AppleScript Pictures 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 Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript Swift AppleScript System Events AppleScript System Events Plist AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text MD 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 savedSearch AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript XMP AppleScript YouTube Applications CityCode github iPhone List lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom Windows zoom