Adobe PDF用のjoboptionsの用紙サイズ変更
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # |
005 | # |
006 | #com.cocolog-nifty.quicktimer.icefloe |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "AppKit" |
011 | use scripting additions |
012 | property refMe : a reference to current application |
013 | |
014 | #解像度今回は使わない |
015 | set listResolution to {"72", "144", "288", "300", "360", "600", "1200", "2400", "3600", "4000"} as list |
016 | #用紙名とサイズのレコード |
017 | set recordPaperSizePt to {|Letter|:{612, 792}, |Legal|:{612, 1008}, |Executive|:{522, 756}, |Ledger|:{1224, 792}, |Tabloid|:{792, 1224}, |Screen|:{468, 373}, |A5|:{420, 595.22}, |A4|:{595.22, 842}, |A3|:{842, 1191}, |A2|:{1191, 1684}, |A1|:{1684, 2384}, |A0|:{2384, 3370}, |A4Extra|:{667, 914}, |A3Extra|:{913, 1262}, |JISB5|:{516, 729}, |JISB4|:{729, 1032}, |JISB3|:{1032, 1460}, |JISB2|:{1460, 2064}, |JISB1|:{2064, 2920}, |JISB0|:{2920, 4127}, |Letter-H|:{612, 792}, |Legal-H|:{612, 1008}, |Executive-H|:{522, 756}, |Ledger-H|:{1224, 792}, |Tabloid-H|:{792, 1224}, |Screen-H|:{468, 373}, |A5-H|:{420, 595.22}, |A4-H|:{595.22, 842}, |A3-H|:{842, 1191}, |A2-H|:{1191, 1684}, |A1-H|:{1684, 2384}, |A0-H|:{2384, 3370}, |A4Extra-H|:{667, 914}, |A3Extra-H|:{913, 1262}, |JISB5-H|:{516, 729}, |JISB4-H|:{729, 1032}, |JISB3-H|:{1032, 1460}, |JISB2-H|:{1460, 2064}, |JISB1-H|:{2064, 2920}, |JISB0-H|:{2920, 4127}} as record |
018 | |
019 | #DICT |
020 | set ocidPaperSizePtDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
021 | ocidPaperSizePtDict's setDictionary:(recordPaperSizePt) |
022 | set ocidAllKeys to ocidPaperSizePtDict's allKeys() |
023 | set ocidSortedArray to ocidAllKeys's sortedArrayUsingSelector:("localizedStandardCompare:") |
024 | set listAllKeys to ocidSortedArray as list |
025 | #ダイアログ |
026 | set strName to (name of current application) as text |
027 | if strName is "osascript" then |
028 | tell application "Finder" to activate |
029 | else |
030 | tell current application to activate |
031 | end if |
032 | set strTitle to ("選んでください") as text |
033 | set strPrompt to ("用紙サイズ選択") as text |
034 | try |
035 | set listResponse to (choose from list listAllKeys with title strTitle with prompt strPrompt default items (item 1 of listAllKeys) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list |
036 | on error |
037 | log "エラーしました" |
038 | return "エラーしました" |
039 | end try |
040 | if (item 1 of listResponse) is false then |
041 | return "キャンセルしましたA" |
042 | else if (item 1 of listResponse) is "キャンセル" then |
043 | return "キャンセルしましたB" |
044 | else |
045 | set strPapaerName to (item 1 of listResponse) as text |
046 | end if |
047 | ## |
048 | set ocidSizeArray to ocidPaperSizePtDict's valueForKey:(strPapaerName) |
049 | |
050 | ############################# |
051 | #ダイアログ |
052 | set strName to (name of current application) as text |
053 | if strName is "osascript" then |
054 | tell application "Finder" to activate |
055 | else |
056 | tell current application to activate |
057 | end if |
058 | set appFileManager to refMe's NSFileManager's defaultManager() |
059 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
060 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
061 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
062 | #UTIリスト |
063 | set listUTI to {"public.item"} |
064 | set strMes to ("ファイルを選んでください") as text |
065 | set strPrompt to ("ファイルを選んでください") as text |
066 | try |
067 | 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 |
068 | on error |
069 | log "Cエラーしました" |
070 | return "Cエラーしました" |
071 | end try |
072 | #パス |
073 | set strFilePath to (POSIX path of aliasFilePath) as text |
074 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
075 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
076 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)) |
077 | #保存用 |
078 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
079 | set strFileName to (ocidBaseFilePathURL's lastPathComponent()) as text |
080 | set strSetFileName to ("(" & strPapaerName & ").joboptions") as text |
081 | set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:(strSetFileName) |
082 | #NSDATA |
083 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
084 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
085 | if (item 2 of listResponse) = (missing value) then |
086 | log "正常処理" |
087 | set ocidReadData to (item 1 of listResponse) |
088 | else if (item 2 of listResponse) ≠ (missing value) then |
089 | set strErrorNO to (item 2 of listResponse)'s code() as text |
090 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
091 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
092 | return "エラーしました" & strErrorNO & strErrorMes |
093 | end if |
094 | #NSString |
095 | set ocidReadString to refMe's NSString's alloc()'s initWithData:(ocidReadData) encoding:(refMe's NSUTF8StringEncoding) |
096 | #NSArray |
097 | set ocidChrSet to refMe's NSCharacterSet's newlineCharacterSet() |
098 | set ocidStringArray to ocidReadString's componentsSeparatedByCharactersInSet:(ocidChrSet) |
099 | #アイテム数 |
100 | set numCntArray to ocidStringArray's |count|() |
101 | #ページサイズは最後の方なので項目は最後から |
102 | repeat with itemNo from (numCntArray - 1) to 0 by -1 |
103 | #リストから取り出す |
104 | set ocidItemString to (ocidStringArray's objectAtIndex:(itemNo)) |
105 | #ページサイズ行がきたら |
106 | set boolContain to (ocidItemString's containsString:("/PageSize")) |
107 | if (boolContain as boolean) is true then |
108 | #サイズw |
109 | set numW to ocidSizeArray's firstObject() |
110 | #サイズh |
111 | set numH to ocidSizeArray's lastObject() |
112 | #新しい値にして |
113 | set strSetSize to ("/PageSize [" & numW & " " & numH & "]") as text |
114 | #Stringにして |
115 | set ocidSetObject to (refMe's NSString's stringWithString:(strSetSize)) |
116 | #Arrayの項目を入れ替え |
117 | (ocidStringArray's replaceObjectAtIndex:(itemNo) withObject:(ocidSetObject)) |
118 | exit repeat |
119 | end if |
120 | end repeat |
121 | #Arrayをテキストにして |
122 | set ocidJoinText to ocidStringArray's componentsJoinedByString:("\n") |
123 | #テキストを保存 |
124 | set ocidOption to (refMe's NSUTF8StringEncoding) |
125 | set listDone to ocidJoinText's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(ocidOption) |error| :(reference) |
126 | #保存結果 |
127 | if (item 1 of listDone) is true then |
128 | log "正常処理" |
129 | else if (item 2 of listDone) ≠ (missing value) then |
130 | set strErrorNO to (item 2 of listDone)'s code() as text |
131 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
132 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
133 | return "エラーしました" & strErrorNO & strErrorMes |
134 | end if |
135 | |
AppleScriptで生成しました |
| 固定リンク