[NSPasteboard]ペーストボード内が画像ならファイルに保存する
ソース | |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | クリップボードの中身で保存可能なものを |
005 | ダウンロードフォルダに保存します |
006 | v2 リンクファイルを追加 |
007 | v2.1 テキストの中身がURLならWEBLOC保存 |
008 | v.2.2 Microsoft Edgeのオプション保存を追加 |
009 | org.microsoft.link-preview |
010 | org.microsoft.titled-hyperlink |
011 | v2.3 選択範囲テキストのフラグメントURLに対応 |
012 | v2.4 ローケーションファイルの拡張子の分岐を作成 |
013 | v2.4.1 画像をコピー等でテキスト無い場合のエラーに対応 |
014 | |
015 | com.cocolog-nifty.quicktimer.icefloe *) |
016 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
017 | use AppleScript version "2.8" |
018 | use framework "Foundation" |
019 | use framework "AppKit" |
020 | use framework "UniformTypeIdentifiers" |
021 | use scripting additions |
022 | |
023 | property refMe : a reference to current application |
024 | |
025 | ################################ |
026 | ##### パス関連 |
027 | ################################ |
028 | ###フォルダ名用に時間を取得する |
029 | set strDateno to doGetDateNo("yyyyMMdd-hhmmss") as text |
030 | ###保存先 |
031 | set appFileManager to refMe's NSFileManager's defaultManager() |
032 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)) |
033 | set ocidDownloadsDirPathURL to ocidURLsArray's firstObject() |
034 | set strSubDirName to ("Pasteboard/" & strDateno) as text |
035 | set ocidSaveParentsDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strSubDirName) isDirectory:(true) |
036 | #フォルダ生成 |
037 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
038 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
039 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveParentsDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
040 | if (item 1 of listDone) is true then |
041 | log "正常処理" |
042 | else if (item 2 of listDone) ≠ (missing value) then |
043 | set strErrorNO to (item 2 of listDone)'s code() as text |
044 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
045 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
046 | return "エラーしました" & strErrorNO & strErrorMes |
047 | end if |
048 | |
049 | ################################ |
050 | ######ペーストボードを取得 |
051 | ################################ |
052 | set ocidPasteboard to refMe's NSPasteboard's generalPasteboard() |
053 | #タイプを取得 |
054 | set ocidPastBoardTypeArray to ocidPasteboard's types() |
055 | log ocidPastBoardTypeArray as list |
056 | |
057 | set strText to ("") |
058 | ######################### |
059 | #【1】FILEURL処理 |
060 | set boolContain to ocidPastBoardTypeArray's containsObject:("NSFilenamesPboardType") |
061 | if boolContain is true then |
062 | set ocidPastBoardData to (ocidPasteboard's propertyListForType:("NSFilenamesPboardType")) |
063 | #パス出力用 |
064 | set ocidFileNamesString to refMe's NSMutableString's alloc()'s init() |
065 | #順番に処理 |
066 | repeat with itemData in ocidPastBoardData |
067 | #出力用のテキストにそのまま追加して改行入れる |
068 | (ocidFileNamesString's appendString:(itemData)) |
069 | (ocidFileNamesString's appendString:("\n")) |
070 | #NSURLにして |
071 | set ocidItemFilePath to itemData's stringByStandardizingPath() |
072 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidItemFilePath)) |
073 | #ファイル名から |
074 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
075 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
076 | set ocidSaveFileName to (ocidBaseFileName's stringByAppendingPathExtension:("fileloc")) |
077 | #保存先のURLを作成 |
078 | set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName) isDirectory:(false)) |
079 | set ocidItemFilePath to ocidFilePathURL's absoluteString() |
080 | #DICTにして |
081 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init() |
082 | (ocidPlistDict's setValue:(ocidItemFilePath) forKey:("URL")) |
083 | #PLISTに |
084 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
085 | set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference)) |
086 | set ocidPlistData to (item 1 of listResponse) |
087 | #保存 |
088 | set ocidOption to (refMe's NSDataWritingAtomic) |
089 | set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference)) |
090 | |
091 | end repeat |
092 | set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:("NSFilenamesPboardType.txt") isDirectory:(false)) |
093 | set listDone to ocidFileNamesString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
094 | end if |
095 | |
096 | ######################### |
097 | #【2】org.chromium.source-url処理 |
098 | set boolContain to ocidPastBoardTypeArray's containsObject:("org.chromium.source-url") |
099 | if boolContain is true then |
100 | set ocidPastBoardData to (ocidPasteboard's dataForType:("org.chromium.source-url")) |
101 | set ocidURLstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding) |
102 | #一度URLにして |
103 | set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLstring)) |
104 | |
105 | #ファイル名用にHOST名を取得 |
106 | set strHostName to ocidURL's |host|() as text |
107 | #保存用拡張子を付与 |
108 | set ocidItemSaveFileName to ("" & strHostName & ".webloc") |
109 | #DICTにして |
110 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init() |
111 | (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL")) |
112 | #PLISTに |
113 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
114 | set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference)) |
115 | set ocidPlistData to (item 1 of listResponse) |
116 | #保存先 |
117 | set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(ocidItemSaveFileName) isDirectory:(false)) |
118 | #保存 |
119 | set ocidOption to (refMe's NSDataWritingAtomic) |
120 | set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference)) |
121 | ##テキストでも保存しておく |
122 | set ocidItemSaveFileName to ("org.chromium.source-url.txt") as text |
123 | set ocidSaveURLFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(ocidItemSaveFileName) isDirectory:(false)) |
124 | # |
125 | set listDone to ocidURLstring's writeToURL:(ocidSaveURLFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
126 | |
127 | end if |
128 | ######################### |
129 | #カレンダー |
130 | set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.calendar.pasteboard.event") |
131 | if boolContain is true then |
132 | set ocidPastBoardData to (ocidPasteboard's dataForType:("com.apple.calendar.pasteboard.event")) |
133 | set ocidEventIDstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding) |
134 | log ocidEventIDstring as text |
135 | end if |
136 | |
137 | set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.iCal.pasteboard.dragOriginDate") |
138 | if boolContain is true then |
139 | set ocidPastBoardData to (ocidPasteboard's dataForType:("com.apple.iCal.pasteboard.dragOriginDate")) |
140 | set ocidEventIDstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding) |
141 | log ocidEventIDstring as text |
142 | end if |
143 | |
144 | ######################### |
145 | #【3】public.url処理 |
146 | set boolContain to ocidPastBoardTypeArray's containsObject:("public.url") |
147 | if boolContain is true then |
148 | set ocidPastBoardData to (ocidPasteboard's dataForType:("public.url")) |
149 | set ocidURLstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding) |
150 | set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLstring)) |
151 | # |
152 | set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidURL) resolvingAgainstBaseURL:(false) |
153 | set strScheme to ocidURLomponents's |scheme|() as text |
154 | if strScheme starts with "http" then |
155 | set strSaveExtension to ("webloc") as text |
156 | else if strScheme starts with "mail" then |
157 | set strSaveExtension to ("mailloc") as text |
158 | else if strScheme starts with "ftp" then |
159 | set strSaveExtension to ("ftploc") as text |
160 | else if strScheme starts with "atp" then |
161 | set strSaveExtension to ("afploc") as text |
162 | else if strScheme starts with "file" then |
163 | set strSaveExtension to ("fileloc") as text |
164 | else if strScheme starts with "news" then |
165 | set strSaveExtension to ("newsloc") as text |
166 | else |
167 | set strSaveExtension to ("inetloc") as text |
168 | end if |
169 | |
170 | set boolContain to ocidPastBoardTypeArray's containsObject:("public.url-name") |
171 | if boolContain is true then |
172 | set ocidPastBoardData to (ocidPasteboard's dataForType:("public.url-name")) |
173 | set ocidURLname to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding) |
174 | set strSaveFileName to ("" & (ocidURLname as text) & "." & strSaveExtension & "") as text |
175 | else if boolContain is false then |
176 | set ocidHostName to ocidURL's |host|() |
177 | set strSaveFileName to ("" & (ocidHostName as text) & "." & strSaveExtension & "") as text |
178 | end if |
179 | #DICTにして |
180 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init() |
181 | (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL")) |
182 | #PLISTに |
183 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
184 | set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference)) |
185 | set ocidPlistData to (item 1 of listResponse) |
186 | #保存先 |
187 | set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
188 | #保存 |
189 | set ocidOption to (refMe's NSDataWritingAtomic) |
190 | set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference)) |
191 | end if |
192 | #全タイプ処理する |
193 | repeat with itemPasteType in ocidPastBoardTypeArray |
194 | #【1】全タイプ処理 拡張子を決めておく |
195 | if (itemPasteType as text) is "public.utf8-plain-text" then |
196 | set strExtension to ("utf8.txt") as text |
197 | else if (itemPasteType as text) is "public.utf16-external-plain-text" then |
198 | set strExtension to ("utf16.txt") as text |
199 | else |
200 | #UTTypeを取得 |
201 | set ocidUTType to (refMe's UTType's typeWithIdentifier:(itemPasteType)) |
202 | #取得できない |
203 | if ocidUTType = (missing value) then |
204 | set strExtension to (missing value) |
205 | else if ocidUTType ≠ (missing value) then |
206 | set ocidExtension to (ocidUTType's preferredFilenameExtension()) |
207 | if ocidExtension = (missing value) then |
208 | set strExtension to (missing value) |
209 | else |
210 | set strExtension to (ocidExtension) as text |
211 | end if |
212 | end if |
213 | end if |
214 | #【2】データを取得 |
215 | if strExtension = (missing value) then |
216 | #拡張子がわからなかったモノは処理しない |
217 | else |
218 | log strExtension |
219 | #データ取り出し |
220 | set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPasteType)) |
221 | if strExtension is "utf8.txt" then |
222 | set ocidTypeClassArray to (refMe's NSMutableArray's alloc()'s init()) |
223 | (ocidTypeClassArray's addObject:(refMe's NSString)) |
224 | set ocidReadString to (ocidPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value)) |
225 | set strText to ocidReadString as text |
226 | end if |
227 | #保存先パス |
228 | #ファイル名はUTI |
229 | set ocidBaseFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(itemPasteType) isDirectory:(false)) |
230 | #拡張子つけて |
231 | set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:(strExtension)) |
232 | #保存 |
233 | set ocidOption to (refMe's NSDataWritingAtomic) |
234 | set listDone to (ocidPastBoardData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)) |
235 | if (item 1 of listDone) is true then |
236 | log "正常処理" |
237 | else if (item 2 of listDone) ≠ (missing value) then |
238 | set strErrorNO to (item 2 of listDone)'s code() as text |
239 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
240 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
241 | return "エラーしました" & strErrorNO & strErrorMes |
242 | end if |
243 | end if |
244 | end repeat |
245 | |
246 | |
247 | |
248 | ################################ |
249 | #テキストがURLならWEBLOC保存を試す |
250 | ################################ |
251 | set ocidURLstring to refMe's NSString's stringWithString:(strText) |
252 | set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\r") withString:("\n")) |
253 | set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n")) |
254 | set ocidLineArray to ocidURLstring's componentsSeparatedByString:("\n") |
255 | repeat with itemLine in ocidLineArray |
256 | set ocidLineString to (itemLine's stringByReplacingOccurrencesOfString:("\t") withString:("")) |
257 | set boolHas to (ocidLineString's hasPrefix:("http")) as boolean |
258 | if boolHas is true then |
259 | set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLstring)) |
260 | set ocidHOST to ocidURL's |host|() |
261 | set strSaveFileName to ("" & (ocidHOST as text) & ".make.webloc") as text |
262 | set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
263 | set ocidURLstring to ocidURL's absoluteString() |
264 | # |
265 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init() |
266 | (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL")) |
267 | #PLIST2NSDATA(MutableContainersAndLeaves) |
268 | set ocidFromat to (refMe's NSPropertyListXMLFormat_v1_0) |
269 | set ocidFromat to (refMe's NSPropertyListBinaryFormat_v1_0) |
270 | set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFromat) options:0 |error|:(reference)) |
271 | set ocidPlistData to (item 1 of listResponse) |
272 | #NSDATA |
273 | set ocidOption to (refMe's NSDataWritingAtomic) |
274 | set listDone to (ocidPlistData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)) |
275 | end if |
276 | end repeat |
277 | |
278 | ################################ |
279 | ##### Microsoft Edgeカスタム |
280 | ################################ |
281 | set ocidPastBoardData to (ocidPasteboard's dataForType:("org.microsoft.link-preview")) |
282 | if ocidPastBoardData ≠ (missing value) then |
283 | set ocidJsonString to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding) |
284 | set strSaveFileName to ("org.microsoft.link-preview.json") as text |
285 | set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
286 | set listDone to ocidJsonString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
287 | end if |
288 | ################################ |
289 | ##### Microsoft Edgeカスタム |
290 | ################################ |
291 | set ocidPastBoardString to (ocidPasteboard's stringForType:("org.microsoft.titled-hyperlink")) |
292 | if ocidPastBoardString ≠ (missing value) then |
293 | set ocidPastBoardString to (ocidPastBoardString's stringByReplacingOccurrencesOfString:("<meta charset='utf-8'>") withString:("")) |
294 | set strSaveFileName to ("org.microsoft.link-preview.html") as text |
295 | set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
296 | set listDone to ocidPastBoardString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
297 | end if |
298 | ################################ |
299 | ##### 保存先を開く |
300 | ################################ |
301 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
302 | set boolDone to appSharedWorkspace's openURL:(ocidSaveParentsDirPathURL) |
303 | |
304 | ################################ |
305 | ##### ファイル名用の時間 |
306 | ################################ |
307 | to doGetDateNo(strDateFormat) |
308 | ####日付情報の取得 |
309 | set ocidDate to current application's NSDate's |date|() |
310 | ###日付のフォーマットを定義 |
311 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
312 | ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX") |
313 | ocidNSDateFormatter's setDateFormat:strDateFormat |
314 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate |
315 | set strDateAndTime to ocidDateAndTime as text |
316 | return strDateAndTime |
317 | end doGetDateNo |
AppleScriptで生成しました |
#!/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 scripting additions
property refMe : a reference to current application
################################
##### パス関連
################################
###ファイル名用に時間を取得する
set strDateno to doGetDateNo("yyyyMMddhhmmss") as text
###保存先
set strFilePath to "~/Desktop/image-" & strDateno & ".tiff" 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 ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
####タイプを取得
set ocidPastBoardTypeArray to ocidPasteboard's types
log ocidPastBoardTypeArray as list
#####イメージがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.tiff"
if boolContain is true then
set ocidTiffData to ocidPasteboard's dataForType:(refMe's NSPasteboardTypeTIFF)
else
return "イメージ以外なので中止"
end if
################################
#####NSimageにして
################################
set ocidImageData to refMe's NSImage's alloc()'s initWithData:ocidTiffData
set ocidTffRep to ocidImageData's TIFFRepresentation
################################
##### 保存
################################
#####TIFF限定ならdataForTypeで取得したデータをそのまま保存でもOK
## set boolFileWrite to (ocidTiffData's writeToURL:ocidFilePathURL atomically:true)
#####保存(PNG変換等も考えて今回はNSImage経由にしてみた)
set boolFileWrite to (ocidTffRep's writeToURL:ocidFilePathURL atomically:true)
################################
##### ファイル名用の時間
################################
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
| 固定リンク
「NSPasteboard」カテゴリの記事
- [NSPasteboard]クリップボードの中身をとにかくファイルに保存するv2.6 (TIFFデータがある場合OCR実行してテキストを取得保存するようにした)(2025.05.29)
- [NSPasteboard]クリップボードの中身をとにかくファイルに保存するv2.5(textClippingフォーマットを追加)(2025.05.25)
- [NSPasteboard]クリップボードの中身をとにかくファイルに保存するv2.2(Microsoft Edgeの個別フォーマットを追加)(2025.04.28)
- クリップボードの中身でファイルに出来るものを全種ファイルで保存するv2(URLのwebloc ファイルのfilelocを追加)(2025.03.11)
- ペーストボードのテキストをプレーンテキストにして再格納(ファイルにできる物はファイルにする機能付き)(2025.02.24)