NSPasteboard

[NSPasteboard]クリップボードの中身をとにかくファイルに保存するv2.6 (TIFFデータがある場合OCR実行してテキストを取得保存するようにした)


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

クリップボードの中身のファイル保存v2.6.applescript
ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004クリップボードの中身で保存可能なものを
005ダウンロードフォルダに保存します
006
007v1初回作成
008v2 リンクファイルを追加
009v2.1 テキストの中身がURLならWEBLOC保存
010v.2.2 Microsoft Edgeのオプション保存を追加
011org.microsoft.link-preview
012org.microsoft.titled-hyperlink
013v2.3 選択範囲テキストのフラグメントURLに対応
014v2.4 ローケーションファイルの拡張子の分岐を作成
015v2.4.1 画像をコピー等でテキスト無い場合のエラーに対応
016v2.5 テキストクリッピングでの保存を追加
017v2.5.1 UTI-Datamissing value対策を入れた
018v2.6 TIFFデータがある場合OCRを実行して結果を保存するようにした
019
020com.cocolog-nifty.quicktimer.icefloe *)
021----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
022use AppleScript version "2.8"
023use framework "Foundation"
024use framework "AppKit"
025use framework "UniformTypeIdentifiers"
026use framework "VisionKit"
027use framework "Vision"
028use scripting additions
029
030property refMe : a reference to current application
031
032################################
033##### パス関連
034################################
035###フォルダ名用に時間を取得する
036set strDateno to doGetDateNo("yyyyMMdd-hhmmss") as text
037###保存先
038set appFileManager to refMe's NSFileManager's defaultManager()
039set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
040set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
041set strSubDirName to ("Pasteboard/" & strDateno) as text
042set ocidSaveParentsDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strSubDirName) isDirectory:(true)
043#フォルダ生成
044set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
045ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
046set listDone to appFileManager's createDirectoryAtURL:(ocidSaveParentsDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
047if (item 1 of listDone) is true then
048   log "正常処理"
049else if (item 2 of listDone) ≠ (missing value) then
050   set strErrorNO to (item 2 of listDone)'s code() as text
051   set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
052   refMe's NSLog("■" & strErrorNO & strErrorMes)
053   return "エラーしました" & strErrorNO & strErrorMes
054end if
055
056################################
057######ペーストボードを取得
058################################
059set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
060#タイプを取得
061set ocidPastBoardTypeArray to ocidPasteboard's types()
062log ocidPastBoardTypeArray as list
063
064set strText to ("")
065#########################
066#【1】FILEURL処理
067set boolContain to ocidPastBoardTypeArray's containsObject:("NSFilenamesPboardType")
068if boolContain is true then
069   set ocidPastBoardData to (ocidPasteboard's propertyListForType:("NSFilenamesPboardType"))
070   #パス出力用
071   set ocidFileNamesString to refMe's NSMutableString's alloc()'s init()
072   #順番に処理
073   repeat with itemData in ocidPastBoardData
074      #出力用のテキストにそのまま追加して改行入れる
075      (ocidFileNamesString's appendString:(itemData))
076      (ocidFileNamesString's appendString:("\n"))
077      #NSURLにして
078      set ocidItemFilePath to itemData's stringByStandardizingPath()
079      set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidItemFilePath))
080      #ファイル名から
081      set ocidFileName to ocidFilePathURL's lastPathComponent()
082      set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
083      set ocidSaveFileName to (ocidBaseFileName's stringByAppendingPathExtension:("fileloc"))
084      #保存先のURLを作成
085      set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName) isDirectory:(false))
086      set ocidItemFilePath to ocidFilePathURL's absoluteString()
087      #DICTにして
088      set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
089      (ocidPlistDict's setValue:(ocidItemFilePath) forKey:("URL"))
090      #PLIST
091      set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
092      set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference))
093      set ocidPlistData to (item 1 of listResponse)
094      #保存
095      set ocidOption to (refMe's NSDataWritingAtomic)
096      set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference))
097      
098   end repeat
099   set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:("NSFilenamesPboardType.txt") isDirectory:(false))
100   set listDone to ocidFileNamesString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
101end if
102
103#########################
104#【2】org.chromium.source-url処理
105set boolContain to ocidPastBoardTypeArray's containsObject:("org.chromium.source-url")
106if boolContain is true then
107   set ocidPastBoardData to (ocidPasteboard's dataForType:("org.chromium.source-url"))
108   set ocidURLstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
109   #一度URLにして
110   set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLstring))
111   
112   #ファイル名用にHOST名を取得
113   set strHostName to ocidURL's |host|() as text
114   #保存用拡張子を付与
115   set ocidItemSaveFileName to ("" & strHostName & ".webloc")
116   #DICTにして
117   set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
118   (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL"))
119   #PLIST
120   set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
121   set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference))
122   set ocidPlistData to (item 1 of listResponse)
123   #保存先
124   set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(ocidItemSaveFileName) isDirectory:(false))
125   #保存
126   set ocidOption to (refMe's NSDataWritingAtomic)
127   set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference))
128   ##テキストでも保存しておく
129   set ocidItemSaveFileName to ("org.chromium.source-url.txt") as text
130   set ocidSaveURLFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(ocidItemSaveFileName) isDirectory:(false))
131   #
132   set listDone to ocidURLstring's writeToURL:(ocidSaveURLFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
133   
134end if
135#########################
136#カレンダー
137set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.calendar.pasteboard.event")
138if boolContain is true then
139   set ocidPastBoardData to (ocidPasteboard's dataForType:("com.apple.calendar.pasteboard.event"))
140   set ocidEventIDstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
141   log ocidEventIDstring as text
142end if
143
144set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.iCal.pasteboard.dragOriginDate")
145if boolContain is true then
146   set ocidPastBoardData to (ocidPasteboard's dataForType:("com.apple.iCal.pasteboard.dragOriginDate"))
147   set ocidEventIDstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
148   log ocidEventIDstring as text
149end if
150
151#########################
152#【3】public.url処理
153set boolContain to ocidPastBoardTypeArray's containsObject:("public.url")
154if boolContain is true then
155   set ocidPastBoardData to (ocidPasteboard's dataForType:("public.url"))
156   set ocidURLstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
157   set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLstring))
158   #
159   set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidURL) resolvingAgainstBaseURL:(false)
160   set strScheme to ocidURLomponents's |scheme|() as text
161   if strScheme starts with "http" then
162      set strSaveExtension to ("webloc") as text
163   else if strScheme starts with "mail" then
164      set strSaveExtension to ("mailloc") as text
165   else if strScheme starts with "ftp" then
166      set strSaveExtension to ("ftploc") as text
167   else if strScheme starts with "atp" then
168      set strSaveExtension to ("afploc") as text
169   else if strScheme starts with "file" then
170      set strSaveExtension to ("fileloc") as text
171   else if strScheme starts with "news" then
172      set strSaveExtension to ("newsloc") as text
173   else
174      set strSaveExtension to ("inetloc") as text
175   end if
176   
177   set boolContain to ocidPastBoardTypeArray's containsObject:("public.url-name")
178   if boolContain is true then
179      set ocidPastBoardData to (ocidPasteboard's dataForType:("public.url-name"))
180      set ocidURLname to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
181      set strSaveFileName to ("" & (ocidURLname as text) & "." & strSaveExtension & "") as text
182   else if boolContain is false then
183      set ocidHostName to ocidURL's |host|()
184      set strSaveFileName to ("" & (ocidHostName as text) & "." & strSaveExtension & "") as text
185   end if
186   #DICTにして
187   set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
188   (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL"))
189   #PLIST
190   set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
191   set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference))
192   set ocidPlistData to (item 1 of listResponse)
193   #保存先
194   set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
195   #保存
196   set ocidOption to (refMe's NSDataWritingAtomic)
197   set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference))
198end if
199
200#########################
201#textClipping
202set ocidTextClippingDict to refMe's NSMutableDictionary's alloc()'s init()
203set ocidUTIDataDict to refMe's NSMutableDictionary's alloc()'s init()
204#全タイプ処理する
205repeat with itemPasteType in ocidPastBoardTypeArray
206   if (itemPasteType as text) is "public.utf8-plain-text" then
207      set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPasteType))
208      (ocidUTIDataDict's setValue:(ocidPastBoardData) forKey:(itemPasteType))
209   else if (itemPasteType as text) is "public.utf16-external-plain-text" then
210      set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPasteType))
211      (ocidUTIDataDict's setValue:(ocidPastBoardData) forKey:(itemPasteType))
212   else if (itemPasteType as text) is "public.utf16-plain-text" then
213      set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPasteType))
214      (ocidUTIDataDict's setValue:(ocidPastBoardData) forKey:(itemPasteType))
215   else if (itemPasteType as text) is "com.apple.traditional-mac-plain-text" then
216      set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPasteType))
217      (ocidUTIDataDict's setValue:(ocidPastBoardData) forKey:(itemPasteType))
218   else if (itemPasteType as text) is "public.rtf" then
219      set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPasteType))
220      set ocidRTFString to (refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding))
221      (ocidUTIDataDict's setValue:(ocidRTFString) forKey:(itemPasteType))
222   else
223      set ocidUTIDataDict to (missing value)
224   end if
225end repeat
226if ocidUTIDataDict ≠ (missing value) then
227   ocidTextClippingDict's setObject:(ocidUTIDataDict) forKey:("UTI-Data")
228   #PLISTデータに
229   set ocidFormat to (refMe's NSPropertyListBinaryFormat_v1_0)
230   set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidTextClippingDict) format:(ocidFormat) options:0 |error|:(reference)
231   set ocidPlistData to (first item of listResponse)
232   #保存先パス
233   set strSaveFileName to ("UTI-Data.textClipping") as text
234   set ocidSaveFilePath to ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)
235   #保存
236   set ocidOption to (refMe's NSDataWritingAtomic)
237   set listDone to (ocidPlistData's writeToURL:(ocidSaveFilePath) options:(ocidOption) |error|:(reference))
238end if
239
240
241#########################
242#全タイプ処理する
243repeat with itemPasteType in ocidPastBoardTypeArray
244   #【1】全タイプ処理 拡張子を決めておく
245   if (itemPasteType as text) is "public.utf8-plain-text" then
246      set strExtension to ("utf8.txt") as text
247   else if (itemPasteType as text) is "public.utf16-external-plain-text" then
248      set strExtension to ("utf16.txt") as text
249   else
250      #UTTypeを取得
251      set ocidUTType to (refMe's UTType's typeWithIdentifier:(itemPasteType))
252      #取得できない
253      if ocidUTType = (missing value) then
254         set strExtension to (missing value)
255      else if ocidUTType ≠ (missing value) then
256         set ocidExtension to (ocidUTType's preferredFilenameExtension())
257         if ocidExtension = (missing value) then
258            set strExtension to (missing value)
259         else
260            set strExtension to (ocidExtension) as text
261         end if
262      end if
263   end if
264   #【2】データを取得
265   if strExtension = (missing value) then
266      #拡張子がわからなかったモノは処理しない
267   else
268      log strExtension
269      #データ取り出し
270      set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPasteType))
271      if strExtension is "utf8.txt" then
272         set ocidTypeClassArray to (refMe's NSMutableArray's alloc()'s init())
273         (ocidTypeClassArray's addObject:(refMe's NSString))
274         set ocidReadString to (ocidPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value))
275         set strText to ocidReadString as text
276      else if strExtension is "tiff" then
277         #TIFFを元にOCRする
278         set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidPastBoardData))
279         set ocidImageRepArray to ocidReadImage's representations()
280         set ocidImageRep to ocidImageRepArray's firstObject()
281         set ocidPxSizeH to ocidImageRep's pixelsHigh()
282         #とっとと解放
283         set ocidImageRepArray to ""
284         set ocidImageRep to ""
285         set intTextHeight to (8 / ocidPxSizeH) as number
286         #####OCR
287         #VNImageRequestHandler's
288         set ocidHandler to (refMe's VNImageRequestHandler's alloc()'s initWithData:(ocidPastBoardData) options:(missing value))
289         #VNRecognizeTextRequest's
290         set ocidRequest to refMe's VNRecognizeTextRequest's alloc()'s init()
291         set ocidOption to (refMe's VNRequestTextRecognitionLevelAccurate)
292         (ocidRequest's setRecognitionLevel:(ocidOption))
293         (ocidRequest's setMinimumTextHeight:(intTextHeight))
294         (ocidRequest's setAutomaticallyDetectsLanguage:(true))
295         (ocidRequest's setRecognitionLanguages:{"en", "ja"})
296         (ocidRequest's setUsesLanguageCorrection:(false))
297         #results
298         (ocidHandler's performRequests:(refMe's NSArray's arrayWithObject:(ocidRequest)) |error|:(reference))
299         set ocidResponseArray to ocidRequest's results()
300         #戻り値を格納するテキスト
301         set ocidFirstOpinionString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
302         set ocidSecondOpinionString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
303         set ocidSaveString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
304         #戻り値の数だけ々
305         repeat with itemArray in ocidResponseArray
306            #候補数指定 110 ここでは2種の候補を戻す指定
307            set ocidRecognizedTextArray to (itemArray's topCandidates:(2))
308            set ocidFirstOpinion to ocidRecognizedTextArray's firstObject()
309            set ocidSecondOpinion to ocidRecognizedTextArray's lastObject()
310            (ocidFirstOpinionString's appendString:(ocidFirstOpinion's |string|()))
311            (ocidFirstOpinionString's appendString:("\n"))
312            (ocidSecondOpinionString's appendString:(ocidSecondOpinion's |string|()))
313            (ocidSecondOpinionString's appendString:("\n"))
314         end repeat
315         ##比較して相違点を探すならここで
316         (ocidSaveString's appendString:("-----第1候補\n\n"))
317         (ocidSaveString's appendString:(ocidFirstOpinionString))
318         (ocidSaveString's appendString:("\n\n-----第2候補\n\n"))
319         (ocidSaveString's appendString:(ocidSecondOpinionString))
320         ###保存
321         set ocidBaseFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(itemPasteType) isDirectory:(false))
322         set ocidSaveTextFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("OCR.txt"))
323         
324         set listDone to (ocidSaveString's writeToURL:(ocidSaveTextFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
325      end if
326      #保存先パス
327      #ファイル名はUTI
328      set ocidBaseFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(itemPasteType) isDirectory:(false))
329      #拡張子つけて
330      set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:(strExtension))
331      #保存
332      set ocidOption to (refMe's NSDataWritingAtomic)
333      set listDone to (ocidPastBoardData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference))
334      if (item 1 of listDone) is true then
335         log "正常処理"
336      else if (item 2 of listDone) ≠ (missing value) then
337         set strErrorNO to (item 2 of listDone)'s code() as text
338         set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
339         refMe's NSLog("■" & strErrorNO & strErrorMes)
340         return "エラーしました" & strErrorNO & strErrorMes
341      end if
342   end if
343end repeat
344
345
346
347################################
348#テキストがURLならWEBLOC保存を試す
349################################
350set ocidURLstring to refMe's NSString's stringWithString:(strText)
351set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
352set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n"))
353set ocidLineArray to ocidURLstring's componentsSeparatedByString:("\n")
354repeat with itemLine in ocidLineArray
355   set ocidLineString to (itemLine's stringByReplacingOccurrencesOfString:("\t") withString:(""))
356   set boolHas to (ocidLineString's hasPrefix:("http")) as boolean
357   if boolHas is true then
358      set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLstring))
359      set ocidHOST to ocidURL's |host|()
360      set strSaveFileName to ("" & (ocidHOST as text) & ".make.webloc") as text
361      set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
362      set ocidURLstring to ocidURL's absoluteString()
363      #
364      set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
365      (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL"))
366      #PLIST2NSDATA(MutableContainersAndLeaves)
367      set ocidFromat to (refMe's NSPropertyListXMLFormat_v1_0)
368      set ocidFromat to (refMe's NSPropertyListBinaryFormat_v1_0)
369      set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFromat) options:0 |error|:(reference))
370      set ocidPlistData to (item 1 of listResponse)
371      #NSDATA
372      set ocidOption to (refMe's NSDataWritingAtomic)
373      set listDone to (ocidPlistData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference))
374   end if
375end repeat
376
377################################
378##### Microsoft Edgeカスタム
379################################
380set ocidPastBoardData to (ocidPasteboard's dataForType:("org.microsoft.link-preview"))
381if ocidPastBoardData ≠ (missing value) then
382   set ocidJsonString to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
383   set strSaveFileName to ("org.microsoft.link-preview.json") as text
384   set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
385   set listDone to ocidJsonString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
386end if
387################################
388##### Microsoft Edgeカスタム
389################################
390set ocidPastBoardString to (ocidPasteboard's stringForType:("org.microsoft.titled-hyperlink"))
391if ocidPastBoardString ≠ (missing value) then
392   set ocidPastBoardString to (ocidPastBoardString's stringByReplacingOccurrencesOfString:("<meta charset='utf-8'>") withString:(""))
393   set strSaveFileName to ("org.microsoft.link-preview.html") as text
394   set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
395   set listDone to ocidPastBoardString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
396end if
397################################
398##### 保存先を開く
399################################
400set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
401set boolDone to appSharedWorkspace's openURL:(ocidSaveParentsDirPathURL)
402
403################################
404##### ファイル名用の時間
405################################
406to doGetDateNo(strDateFormat)
407   ####日付情報の取得
408   set ocidDate to current application's NSDate's |date|()
409   ###日付のフォーマットを定義
410   set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
411   ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
412   ocidNSDateFormatter's setDateFormat:strDateFormat
413   set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
414   set strDateAndTime to ocidDateAndTime as text
415   return strDateAndTime
416end doGetDateNo
AppleScriptで生成しました

| | コメント (0)

[NSPasteboard]クリップボードの中身をとにかくファイルに保存するv2.5(textClippingフォーマットを追加)

アップデートしました
[NSPasteboard]クリップボードの中身をとにかくファイルに保存するv2.6 (TIFFデータがある場合OCR実行してテキストを取得保存するようにした)

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

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

| | コメント (0)

[NSPasteboard]クリップボードの中身をとにかくファイルに保存するv2.2(Microsoft Edgeの個別フォーマットを追加)



修正版があります
[NSPasteboard]クリップボードの中身をとにかくファイルに保存するv2.5(textClippingフォーマットを追加)


org.microsoft.link-preview
org.microsoft.titled-hyperlinkを追加で保存できるなら保存するようにした


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

クリップボードの中身のファイル保存v2.4.applescript
ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004クリップボードの中身で保存可能なものを
005ダウンロードフォルダに保存します
006v2 リンクファイルを追加
007v2.1 テキストの中身がURLならWEBLOC保存
008v.2.2 Microsoft Edgeのオプション保存を追加
009org.microsoft.link-preview
010org.microsoft.titled-hyperlink
011v2.3 選択範囲テキストのフラグメントURLに対応
012v2.4 ローケーションファイルの拡張子の分岐を作成
013v2.4.1 画像をコピー等でテキスト無い場合のエラーに対応
014
015com.cocolog-nifty.quicktimer.icefloe *)
016----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
017use AppleScript version "2.8"
018use framework "Foundation"
019use framework "AppKit"
020use framework "UniformTypeIdentifiers"
021use scripting additions
022
023property refMe : a reference to current application
024
025################################
026##### パス関連
027################################
028###フォルダ名用に時間を取得する
029set strDateno to doGetDateNo("yyyyMMdd-hhmmss") as text
030###保存先
031set appFileManager to refMe's NSFileManager's defaultManager()
032set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
033set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
034set strSubDirName to ("Pasteboard/" & strDateno) as text
035set ocidSaveParentsDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strSubDirName) isDirectory:(true)
036#フォルダ生成
037set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
038ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
039set listDone to appFileManager's createDirectoryAtURL:(ocidSaveParentsDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
040if (item 1 of listDone) is true then
041   log "正常処理"
042else 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
047end if
048
049################################
050######ペーストボードを取得
051################################
052set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
053#タイプを取得
054set ocidPastBoardTypeArray to ocidPasteboard's types()
055log ocidPastBoardTypeArray as list
056
057set strText to ("")
058#########################
059#【1】FILEURL処理
060set boolContain to ocidPastBoardTypeArray's containsObject:("NSFilenamesPboardType")
061if 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)
094end if
095
096#########################
097#【2】org.chromium.source-url処理
098set boolContain to ocidPastBoardTypeArray's containsObject:("org.chromium.source-url")
099if 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   
127end if
128#########################
129#カレンダー
130set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.calendar.pasteboard.event")
131if 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
135end if
136
137set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.iCal.pasteboard.dragOriginDate")
138if 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
142end if
143
144#########################
145#【3】public.url処理
146set boolContain to ocidPastBoardTypeArray's containsObject:("public.url")
147if 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))
191end if
192#全タイプ処理する
193repeat 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
244end repeat
245
246
247
248################################
249#テキストがURLならWEBLOC保存を試す
250################################
251set ocidURLstring to refMe's NSString's stringWithString:(strText)
252set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
253set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n"))
254set ocidLineArray to ocidURLstring's componentsSeparatedByString:("\n")
255repeat 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
276end repeat
277
278################################
279##### Microsoft Edgeカスタム
280################################
281set ocidPastBoardData to (ocidPasteboard's dataForType:("org.microsoft.link-preview"))
282if 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)
287end if
288################################
289##### Microsoft Edgeカスタム
290################################
291set ocidPastBoardString to (ocidPasteboard's stringForType:("org.microsoft.titled-hyperlink"))
292if 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)
297end if
298################################
299##### 保存先を開く
300################################
301set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
302set boolDone to appSharedWorkspace's openURL:(ocidSaveParentsDirPathURL)
303
304################################
305##### ファイル名用の時間
306################################
307to 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
317end doGetDateNo
AppleScriptで生成しました

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

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

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

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

| | コメント (0)

クリップボードの中身でファイルに出来るものを全種ファイルで保存するv2(URLのwebloc ファイルのfilelocを追加)

デスクトップ・ウィジェット用
こんな感じで使っています
Thumbnail

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

クリップボードの中身のファイル保存v2.4.applescript
ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004クリップボードの中身で保存可能なものを
005ダウンロードフォルダに保存します
006v2 リンクファイルを追加
007v2.1 テキストの中身がURLならWEBLOC保存
008v.2.2 Microsoft Edgeのオプション保存を追加
009org.microsoft.link-preview
010org.microsoft.titled-hyperlink
011v2.3 選択範囲テキストのフラグメントURLに対応
012v2.4 ローケーションファイルの拡張子の分岐を作成
013v2.4.1 画像をコピー等でテキスト無い場合のエラーに対応
014
015com.cocolog-nifty.quicktimer.icefloe *)
016----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
017use AppleScript version "2.8"
018use framework "Foundation"
019use framework "AppKit"
020use framework "UniformTypeIdentifiers"
021use scripting additions
022
023property refMe : a reference to current application
024
025################################
026##### パス関連
027################################
028###フォルダ名用に時間を取得する
029set strDateno to doGetDateNo("yyyyMMdd-hhmmss") as text
030###保存先
031set appFileManager to refMe's NSFileManager's defaultManager()
032set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
033set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
034set strSubDirName to ("Pasteboard/" & strDateno) as text
035set ocidSaveParentsDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strSubDirName) isDirectory:(true)
036#フォルダ生成
037set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
038ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
039set listDone to appFileManager's createDirectoryAtURL:(ocidSaveParentsDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
040if (item 1 of listDone) is true then
041   log "正常処理"
042else 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
047end if
048
049################################
050######ペーストボードを取得
051################################
052set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
053#タイプを取得
054set ocidPastBoardTypeArray to ocidPasteboard's types()
055log ocidPastBoardTypeArray as list
056
057set strText to ("")
058#########################
059#【1】FILEURL処理
060set boolContain to ocidPastBoardTypeArray's containsObject:("NSFilenamesPboardType")
061if 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)
094end if
095
096#########################
097#【2】org.chromium.source-url処理
098set boolContain to ocidPastBoardTypeArray's containsObject:("org.chromium.source-url")
099if 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   
127end if
128#########################
129#カレンダー
130set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.calendar.pasteboard.event")
131if 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
135end if
136
137set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.iCal.pasteboard.dragOriginDate")
138if 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
142end if
143
144#########################
145#【3】public.url処理
146set boolContain to ocidPastBoardTypeArray's containsObject:("public.url")
147if 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))
191end if
192#全タイプ処理する
193repeat 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
244end repeat
245
246
247
248################################
249#テキストがURLならWEBLOC保存を試す
250################################
251set ocidURLstring to refMe's NSString's stringWithString:(strText)
252set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
253set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n"))
254set ocidLineArray to ocidURLstring's componentsSeparatedByString:("\n")
255repeat 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
276end repeat
277
278################################
279##### Microsoft Edgeカスタム
280################################
281set ocidPastBoardData to (ocidPasteboard's dataForType:("org.microsoft.link-preview"))
282if 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)
287end if
288################################
289##### Microsoft Edgeカスタム
290################################
291set ocidPastBoardString to (ocidPasteboard's stringForType:("org.microsoft.titled-hyperlink"))
292if 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)
297end if
298################################
299##### 保存先を開く
300################################
301set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
302set boolDone to appSharedWorkspace's openURL:(ocidSaveParentsDirPathURL)
303
304################################
305##### ファイル名用の時間
306################################
307to 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
317end doGetDateNo
AppleScriptで生成しました

Thumbnail

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

クリップボードの中身のファイル保存v2.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# クリップボードの中身で保存可能なものを
005#   ダウンロードフォルダに保存します
006#   v2 リンクファイルを追加
007# com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use framework "UniformTypeIdentifiers"
013use scripting additions
014
015property refMe : a reference to current application
016
017################################
018##### パス関連
019################################
020###フォルダ名用に時間を取得する
021set strDateno to doGetDateNo("yyyyMMdd-hhmmss") as text
022###保存先
023set appFileManager to refMe's NSFileManager's defaultManager()
024set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
025set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
026set strSubDirName to ("Pasteboard/" & strDateno) as text
027set ocidSaveParentsDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strSubDirName) isDirectory:(true)
028#フォルダ生成
029set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
030ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
031set listDone to appFileManager's createDirectoryAtURL:(ocidSaveParentsDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
032if (item 1 of listDone) is true then
033   log "正常処理"
034else if (item 2 of listDone) ≠ (missing value) then
035   set strErrorNO to (item 2 of listDone)'s code() as text
036   set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
037   refMe's NSLog("■" & strErrorNO & strErrorMes)
038   return "エラーしました" & strErrorNO & strErrorMes
039end if
040
041################################
042######ペーストボードを取得
043################################
044set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
045#タイプを取得
046set ocidPastBoardTypeArray to ocidPasteboard's types()
047#########################
048#【1】FILEURL処理
049set boolContain to ocidPastBoardTypeArray's containsObject:("NSFilenamesPboardType")
050if boolContain is true then
051   set ocidPastBoardData to (ocidPasteboard's propertyListForType:("NSFilenamesPboardType"))
052   #パス出力用
053   set ocidFileNamesString to refMe's NSMutableString's alloc()'s init()
054   #順番に処理
055   repeat with itemData in ocidPastBoardData
056      #出力用のテキストにそのまま追加して改行入れる
057      (ocidFileNamesString's appendString:(itemData))
058      (ocidFileNamesString's appendString:("\n"))
059      #NSURLにして
060      set ocidItemFilePath to itemData's stringByStandardizingPath()
061      set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidItemFilePath))
062      #ファイル名から
063      set ocidFileName to ocidFilePathURL's lastPathComponent()
064      set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
065      set ocidSaveFileName to (ocidBaseFileName's stringByAppendingPathExtension:("fileloc"))
066      #保存先のURLを作成
067      set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName) isDirectory:(false))
068      set ocidItemFilePath to ocidFilePathURL's absoluteString()
069      #DICTにして
070      set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
071      (ocidPlistDict's setValue:(ocidItemFilePath) forKey:("URL"))
072      #PLIST
073      set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
074      set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference))
075      set ocidPlistData to (item 1 of listResponse)
076      #保存
077      set ocidOption to (refMe's NSDataWritingAtomic)
078      set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference))
079      
080   end repeat
081   set ocidSaveFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:("NSFilenamesPboardType.txt") isDirectory:(false))
082   set listDone to ocidFileNamesString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
083end if
084
085#########################
086#【2】org.chromium.source-url処理
087set boolContain to ocidPastBoardTypeArray's containsObject:("org.chromium.source-url")
088if boolContain is true then
089   set ocidPastBoardData to (ocidPasteboard's dataForType:("org.chromium.source-url"))
090   set ocidURLstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
091   #一度URLにして
092   set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLstring))
093   #ファイル名用にHOST名を取得
094   set strHostName to ocidURL's |host|() as text
095   #保存用拡張子を付与
096   set ocidItemSaveFileName to ("" & strHostName & ".webloc")
097   #DICTにして
098   set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
099   (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL"))
100   #PLIST
101   set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
102   set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference))
103   set ocidPlistData to (item 1 of listResponse)
104   #保存先
105   set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(ocidItemSaveFileName) isDirectory:(false))
106   #保存
107   set ocidOption to (refMe's NSDataWritingAtomic)
108   set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference))
109end if
110
111
112#########################
113#【3】public.url処理
114set boolContain to ocidPastBoardTypeArray's containsObject:("public.url")
115if boolContain is true then
116   set ocidPastBoardData to (ocidPasteboard's dataForType:("public.url"))
117   set ocidURLstring to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
118   set ocidURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLstring))
119   set boolContain to ocidPastBoardTypeArray's containsObject:("public.url-name")
120   if boolContain is true then
121      set ocidPastBoardData to (ocidPasteboard's dataForType:("public.url-name"))
122      set ocidURLname to refMe's NSString's alloc()'s initWithData:(ocidPastBoardData) encoding:(refMe's NSUTF8StringEncoding)
123      set strSaveFileName to ("" & (ocidURLname as text) & ".webloc") as text
124   else if boolContain is false then
125      set ocidHostName to ocidURL's |host|()
126      set strSaveFileName to ("" & (ocidHostName as text) & ".webloc") as text
127   end if
128   #DICTにして
129   set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s init()
130   (ocidPlistDict's setValue:(ocidURLstring) forKey:("URL"))
131   #PLIST
132   set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
133   set listResponse to (refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidFormat) options:0 |error|:(reference))
134   set ocidPlistData to (item 1 of listResponse)
135   #保存先
136   set ocidPlistFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
137   #保存
138   set ocidOption to (refMe's NSDataWritingAtomic)
139   set listDone to (ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference))
140end if
141
142
143#全タイプ処理する
144repeat with itemPasteType in ocidPastBoardTypeArray
145   #【1】全タイプ処理 拡張子を決めておく
146   if (itemPasteType as text) is "public.utf8-plain-text" then
147      set strExtension to ("utf8.txt") as text
148   else if (itemPasteType as text) is "public.utf16-external-plain-text" then
149      set strExtension to ("utf16.txt") as text
150   else
151      #UTTypeを取得
152      set ocidUTType to (refMe's UTType's typeWithIdentifier:(itemPasteType))
153      #取得できない
154      if ocidUTType = (missing value) then
155         set strExtension to (missing value)
156      else if ocidUTType ≠ (missing value) then
157         set ocidExtension to (ocidUTType's preferredFilenameExtension())
158         if ocidExtension = (missing value) then
159            set strExtension to (missing value)
160         else
161            set strExtension to (ocidExtension) as text
162         end if
163      end if
164   end if
165   #【2】データを取得
166   if strExtension = (missing value) then
167      #拡張子がわからなかったモノは処理しない
168   else
169      log strExtension
170      #データ取り出し
171      set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPasteType))
172      if strExtension is "utf8.txt" then
173         set ocidTypeClassArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
174         (ocidTypeClassArray's addObject:(refMe's NSString))
175         set ocidReadString to (ocidPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value))
176         set strText to ocidReadString as text
177      end if
178      #保存先パス
179      #ファイル名はUTI
180      set ocidBaseFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(itemPasteType) isDirectory:(false))
181      #拡張子つけて
182      set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:(strExtension))
183      #保存
184      set ocidOption to (refMe's NSDataWritingAtomic)
185      set listDone to (ocidPastBoardData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference))
186      if (item 1 of listDone) is true then
187         log "正常処理"
188      else if (item 2 of listDone) ≠ (missing value) then
189         set strErrorNO to (item 2 of listDone)'s code() as text
190         set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
191         refMe's NSLog("■" & strErrorNO & strErrorMes)
192         return "エラーしました" & strErrorNO & strErrorMes
193      end if
194   end if
195end repeat
196
197
198################################
199##### 保存先を開く
200################################
201set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
202set boolDone to appSharedWorkspace's openURL:(ocidSaveParentsDirPathURL)
203
204
205################################
206##### ファイル名用の時間
207################################
208to doGetDateNo(strDateFormat)
209   ####日付情報の取得
210   set ocidDate to current application's NSDate's |date|()
211   ###日付のフォーマットを定義
212   set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
213   ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
214   ocidNSDateFormatter's setDateFormat:strDateFormat
215   set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
216   set strDateAndTime to ocidDateAndTime as text
217   return strDateAndTime
218end doGetDateNo
AppleScriptで生成しました

|

ペーストボードのテキストをプレーンテキストにして再格納(ファイルにできる物はファイルにする機能付き)

ショートカット(Shortcuts)カット用
テキストデータはプレーンテキストにしてから再格納
画像とかのデータだったらファイルにしちゃう機能付き

202502240742191_1020x1682

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

クリップボードの中身のファイル保存v2.4.applescript
ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004クリップボードの中身で保存可能なものを
005ダウンロードフォルダに保存します
006v2 リンクファイルを追加
007v2.1 テキストの中身がURLならWEBLOC保存
008v.2.2 Microsoft Edgeのオプション保存を追加
009org.microsoft.link-preview
010org.microsoft.titled-hyperlink
011v2.3 選択範囲テキストのフラグメントURLに対応
012v2.4 ローケーションファイルの拡張子の分岐を作成
013v2.4.1 画像をコピー等でテキスト無い場合のエラーに対応
014
015com.cocolog-nifty.quicktimer.icefloe *)
016----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
017use AppleScript version "2.8"
018use framework "Foundation"
019use framework "AppKit"
020use framework "UniformTypeIdentifiers"
021use scripting additions
022
023property refMe : a reference to current application
024
025################################
026##### パス関連
027################################
028###フォルダ名用に時間を取得する
029set strDateno to doGetDateNo("yyyyMMdd-hhmmss") as text
030###保存先
031set appFileManager to refMe's NSFileManager's defaultManager()
032set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
033set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
034set strSubDirName to ("Pasteboard/" & strDateno) as text
035set ocidSaveParentsDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strSubDirName) isDirectory:(true)
036#フォルダ生成
037set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
038ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
039set listDone to appFileManager's createDirectoryAtURL:(ocidSaveParentsDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
040if (item 1 of listDone) is true then
041   log "正常処理"
042else 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
047end if
048
049################################
050######ペーストボードを取得
051################################
052set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
053#タイプを取得
054set ocidPastBoardTypeArray to ocidPasteboard's types()
055log ocidPastBoardTypeArray as list
056
057set strText to ("")
058#########################
059#【1】FILEURL処理
060set boolContain to ocidPastBoardTypeArray's containsObject:("NSFilenamesPboardType")
061if 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)
094end if
095
096#########################
097#【2】org.chromium.source-url処理
098set boolContain to ocidPastBoardTypeArray's containsObject:("org.chromium.source-url")
099if 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   
127end if
128#########################
129#カレンダー
130set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.calendar.pasteboard.event")
131if 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
135end if
136
137set boolContain to ocidPastBoardTypeArray's containsObject:("com.apple.iCal.pasteboard.dragOriginDate")
138if 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
142end if
143
144#########################
145#【3】public.url処理
146set boolContain to ocidPastBoardTypeArray's containsObject:("public.url")
147if 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))
191end if
192#全タイプ処理する
193repeat 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
244end repeat
245
246
247
248################################
249#テキストがURLならWEBLOC保存を試す
250################################
251set ocidURLstring to refMe's NSString's stringWithString:(strText)
252set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
253set ocidURLstring to (ocidURLstring's stringByReplacingOccurrencesOfString:("\n\n") withString:("\n"))
254set ocidLineArray to ocidURLstring's componentsSeparatedByString:("\n")
255repeat 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
276end repeat
277
278################################
279##### Microsoft Edgeカスタム
280################################
281set ocidPastBoardData to (ocidPasteboard's dataForType:("org.microsoft.link-preview"))
282if 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)
287end if
288################################
289##### Microsoft Edgeカスタム
290################################
291set ocidPastBoardString to (ocidPasteboard's stringForType:("org.microsoft.titled-hyperlink"))
292if 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)
297end if
298################################
299##### 保存先を開く
300################################
301set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
302set boolDone to appSharedWorkspace's openURL:(ocidSaveParentsDirPathURL)
303
304################################
305##### ファイル名用の時間
306################################
307to 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
317end doGetDateNo
AppleScriptで生成しました

ペーストボードの中身をテキストにそれ以外なら保存.scpt

AppleScript サンプルコード

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

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
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use framework "UniformTypeIdentifiers"
012use scripting additions
013
014property refMe : a reference to current application
015
016################################
017##### パス関連
018################################
019###フォルダ名用に時間を取得する
020set strDateno to doGetDateNo("yyyyMMdd-hhmmss") as text
021###保存先
022set appFileManager to refMe's NSFileManager's defaultManager()
023set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
024set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
025set strSubDirName to ("Pasteboard/" & strDateno) as text
026set ocidSaveParentsDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strSubDirName) isDirectory:(true)
027#フォルダ生成
028set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
029ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
030set listDone to appFileManager's createDirectoryAtURL:(ocidSaveParentsDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)
031if (item 1 of listDone) is true then
032  log "正常処理"
033else if (item 2 of listDone) (missing value) then
034  set strErrorNO to (item 2 of listDone)'s code() as text
035  set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
036  refMe's NSLog("■:" & strErrorNO & strErrorMes)
037  return "エラーしました" & strErrorNO & strErrorMes
038end if
039
040################################
041######ペーストボードを取得
042################################
043set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
044#タイプを取得
045set ocidPastBoardTypeArray to ocidPasteboard's types()
046#全タイプ処理する
047repeat with itemPastType in ocidPastBoardTypeArray
048  #【1】全タイプ処理 拡張子を決めておく
049  if (itemPastType as text) is "public.utf8-plain-text" then
050    set strExtension to ("utf8.txt") as text
051  else if (itemPastType as text) is "public.utf16-external-plain-text" then
052    set strExtension to ("utf16.txt") as text
053  else
054    #UTTypeを取得
055    set ocidUTType to (refMe's UTType's typeWithIdentifier:(itemPastType))
056    #取得できない
057    if ocidUTType = (missing value) then
058      set strExtension to (missing value)
059    else if ocidUTType  (missing value) then
060      set ocidExtension to (ocidUTType's preferredFilenameExtension())
061      if ocidExtension = (missing value) then
062        set strExtension to (missing value)
063      else
064        set strExtension to (ocidExtension) as text
065      end if
066    end if
067  end if
068  #【2】データを取得
069  if strExtension = (missing value) then
070    #拡張子がわからなかったモノは処理しない
071  else
072    log strExtension
073    #データ取り出し
074    set ocidPastBoardData to (ocidPasteboard's dataForType:(itemPastType))
075    if strExtension is "utf8.txt" then
076      set ocidTypeClassArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
077      (ocidTypeClassArray's addObject:(refMe's NSString))
078      set ocidReadString to (ocidPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value))
079      set strText to ocidReadString as text
080    end if
081    #保存先パス
082    #ファイル名はUTI
083    set ocidBaseFilePathURL to (ocidSaveParentsDirPathURL's URLByAppendingPathComponent:(itemPastType) isDirectory:(false))
084    #拡張子つけて
085    set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:(strExtension))
086    #保存
087    set ocidOption to (refMe's NSDataWritingAtomic)
088    set listDone to (ocidPastBoardData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference))
089    if (item 1 of listDone) is true then
090      log "正常処理"
091    else if (item 2 of listDone) (missing value) then
092      set strErrorNO to (item 2 of listDone)'s code() as text
093      set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
094      refMe's NSLog("■:" & strErrorNO & strErrorMes)
095      return "エラーしました" & strErrorNO & strErrorMes
096    end if
097  end if
098end repeat
099
100################################
101#テキストをプレインテキストにして
102#格納しなおす
103################################
104
105try
106  set strText to text returned of recordResult as text
107  ####ペーストボード宣言
108  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
109  set ocidText to (refMe's NSString's stringWithString:(strText))
110  appPasteboard's clearContents()
111  appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
112on error
113  tell application "Finder"
114    set the clipboard to strText as text
115  end tell
116end try
117
118################################
119##### 保存先を開く
120################################
121set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
122set boolDone to appSharedWorkspace's openURL:(ocidSaveParentsDirPathURL)
123
124
125################################
126##### ファイル名用の時間
127################################
128to doGetDateNo(strDateFormat)
129  ####日付情報の取得
130  set ocidDate to current application's NSDate's |date|()
131  ###日付のフォーマットを定義
132  set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
133  ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
134  ocidNSDateFormatter's setDateFormat:strDateFormat
135  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
136  set strDateAndTime to ocidDateAndTime as text
137  return strDateAndTime
138end doGetDateNo
AppleScriptで生成しました

|

[NSOpenPanel]対象のファイルのUTIを限定する『開く』パネル


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#
006#
007# com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use framework "UniformTypeIdentifiers"
013use scripting additions
014property refMe : a reference to current application
015property ocidChosenPathURL : missing value
016
017on appChooseFolder:(ocidArgDirPathURL)
018  #
019  set ocidOpenPane to refMe's NSOpenPanel's openPanel()
020  #
021  ocidOpenPane's setDirectoryURL:(ocidArgDirPathURL)
022  ocidOpenPane's setCanChooseFiles:(true)
023  ocidOpenPane's setCanChooseDirectories:(true)
024  ocidOpenPane's setAllowsMultipleSelection:(false)
025  ocidOpenPane's setAccessoryViewDisclosed:(true)
026  ocidOpenPane's setTitle:"タイトルはやくしない"
027  ocidOpenPane's setPrompt:"実行しちゃうよ♪"
028  ocidOpenPane's setMessage:"ファイルかフォルダ選んでね"
029  ocidOpenPane's setShowsTagField:(true)
030  ocidOpenPane's setResolvesAliases:(false)
031  ocidOpenPane's setShowsHiddenFiles:(true)
032  ocidOpenPane's setExtensionHidden:(false)
033  ocidOpenPane's setCanCreateDirectories:(true)
034  ocidOpenPane's setCanDownloadUbiquitousContents:(true)
035  ocidOpenPane's setCanResolveUbiquitousConflicts:(true)
036  ocidOpenPane's setCanSelectHiddenExtension:(true)
037  ocidOpenPane's setTreatsFilePackagesAsDirectories:(true)
038  #対象のUTIを限定する
039  set listUTI to {"public.image", "public.tiff", "public.jpeg", "public.png"} as list
040  set ocidUTIarray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
041  repeat with itemUTI in listUTI
042    set ocidItemUTType to (refMe's UTType's typeWithIdentifier:(itemUTI))
043    (ocidUTIarray's addObject:(ocidItemUTType))
044  end repeat
045  #UTTypeを限定項目としてセット
046  ocidOpenPane's setAllowedContentTypes:(ocidUTIarray)
047  #
048  set returnCode to ocidOpenPane's runModal()
049  #キャンセルをmissing value
050  if returnCode = (refMe's NSFileHandlingPanelCancelButton) then
051    error number -128
052  end if
053  #戻しURL
054  set my ocidChosenPathURL to ocidOpenPane's |URL|()
055end appChooseFolder:
056
057##デフォルトパス
058set aliasPathToMe to (path to me) as alias
059set strPathToMe to (POSIX path of aliasPathToMe) as text
060set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe)
061set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath()
062set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false)
063set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent()
064##ダイアログ呼び出し
065my performSelectorOnMainThread:("appChooseFolder:") withObject:(ocidContainerDirPathURL) waitUntilDone:(true)
066##戻り値NULならキャンセルで停止
067if ocidChosenPathURL = (missing value) then
068  error number -128
069else
070  ##戻り値あるならURL
071  set ocidChosenPathURL to ocidChosenPathURL
072  set strFilePath to ocidChosenPathURL's absoluteString() as text
073  log strFilePath
074end if
075
076
AppleScriptで生成しました

|

クリップボード内のテキストに改行を<br />に変更して戻す 修正


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use scripting additions
010
011property refMe : a reference to current application
012property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
013
014########################
015## クリップボードの中身取り出し
016###初期化
017set appPasteboard to refMe's NSPasteboard's generalPasteboard()
018set ocidPastBoardTypeArray to appPasteboard's types
019###テキストがあれば
020set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
021if boolContain = true then
022  ###値を格納する
023  tell application "Finder"
024    set strReadString to (the clipboard as text) as text
025  end tell
026else
027  set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
028  if boolContain = true then
029    set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
030    set strReadString to ocidReadString as text
031  end if
032end if
033########################
034#
035set ocidReadString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
036set ocidSetValue to refMe's NSString's stringWithString:(strReadString)
037ocidReadString's setString:(ocidSetValue)
038############################
039#改行コード判定
040set ocidCrRange to (ocidReadString's rangeOfString:("\r"))'s location()
041set ocidLfRange to (ocidReadString's rangeOfString:("\n"))'s location()
042if ocidCrRange = refNSNotFound then
043  log "Mac改行は無い"
044  if ocidLfRange = refNSNotFound then
045    log "Mac改行もUNIX改行は無い=改行の無い1行か空のテキスト"
046    set ocidLength to ocidReadString's |length|
047    if ocidLength = (refMe's NSNumber's numberWithInteger:0) then
048      log "空のテキスト UNIX改行をセットします"
049      set ocidNewLineChar to (refMe's NSString's stringWithString:("\n"))
050    else
051      log "改行無しの1行テキスト UNIX改行をセットします"
052      set ocidNewLineChar to (refMe's NSString's stringWithString:("\n"))
053    end if
054  else
055    log "Mac改行無+UNIX改行あり=UNIX改行"
056    set ocidNewLineChar to (refMe's NSString's stringWithString:("\n"))
057  end if
058else
059  if ocidLfRange = refNSNotFound then
060    log "MacがあるUNIX改行は無い=Mac改行"
061    set ocidNewLineChar to (refMe's NSString's stringWithString:("\r"))
062  else
063    log "Mac改行ある+UNIX改行あり=Windows改行"
064    set ocidNewLineChar to (refMe's NSString's stringWithString:("\r\n"))
065  end if
066end if
067#改行毎でリストにする
068set ocidLineArray to (ocidReadString's componentsSeparatedByString:(ocidNewLineChar))
069############################
070#戻し用のテキスト
071set ocidSaveString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
072#
073repeat with itemArray in ocidLineArray
074  #行テキスト
075  (ocidSaveString's appendString:(itemArray))
076  #<BR />
077  set ocidSetValue to (refMe's NSString's stringWithString:("<br />"))
078  (ocidSaveString's appendString:(ocidSetValue))
079  #改行
080  (ocidSaveString's appendString:(ocidNewLineChar))
081end repeat
082
083set strSaveString to ocidSaveString as text
084
085
086
087###ダイアログを前面に出す
088tell current application
089  set strName to name as text
090end tell
091####スクリプトメニューから実行したら
092if strName is "osascript" then
093  tell application "Finder"
094    activate
095  end tell
096else
097  tell current application
098    activate
099  end tell
100end if
101###ICON
102tell application "Finder"
103  set aliasIconPath to (POSIX file "/System/Applications/TextEdit.app/Contents/Resources/AppIcon.icns") as alias
104end tell
105set strTitle to ("戻り値です") as text
106set strMes to ("<br />入れました") as text
107set recordResult to (display dialog strMes with title strTitle default answer strSaveString buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" giving up after 20 with icon aliasIconPath without hidden answer)
108
109if button returned of recordResult is "クリップボードにコピー" then
110  set strText to text returned of recordResult as text
111  ####ペーストボード宣言
112  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
113  set ocidText to (refMe's NSString's stringWithString:(strText))
114  appPasteboard's clearContents()
115  appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
116end if
AppleScriptで生成しました

|

ペーストボードの内容の種類の確認


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use scripting additions
010
011property refMe : a reference to current application
012
013
014################################
015######ペーストボードを取得
016################################
017set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
018####タイプを取得
019set ocidPastBoardTypeArray to ocidPasteboard's types
020#ダイアログに戻すテキスト
021set strOutPutText to ("") as text
022#順番に確認
023repeat with itemType in ocidPastBoardTypeArray
024#タイプをテキストで
025  set strType to itemType as text
026#追加していく
027  set strOutPutText to (strOutPutText & strType & "\n") as text
028end repeat
029
030
031##############################
032#####ダイアログ
033##############################
034tell current application
035  set strName to name as text
036end tell
037if strName is "osascript" then
038  tell application "Finder"
039    activate
040  end tell
041else
042  tell current application
043    activate
044  end tell
045end if
046set strFilePath to ("/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AllMyFiles.icns") as text
047set aliasIconPath to (POSIX file strFilePath) as alias
048try
049  set recordResult to (display dialog "戻り値です" with title "戻り値です" default answer strOutPutText buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record
050on error
051  return "エラーしました"
052end try
053if (gave up of recordResult) is true then
054  return "時間切れです"
055end if
056##############################
057#####自分自身を再実行
058##############################
059if button returned of recordResult is "再実行" then
060  tell application "Finder"
061    set aliasPathToMe to (path to me) as alias
062  end tell
063  run script aliasPathToMe with parameters "再実行"
064end if
065##############################
066#####値のコピー
067##############################
068if button returned of recordResult is "クリップボードにコピー" then
069  try
070    set strText to text returned of recordResult as text
071    ####ペーストボード宣言
072    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
073    set ocidText to (refMe's NSString's stringWithString:(strText))
074    appPasteboard's clearContents()
075    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
076  on error
077    tell application "Finder"
078      set the clipboard to strText as text
079    end tell
080  end try
081end if
082
083
084return 0
AppleScriptで生成しました

|

[NSPasteboard]ペーストボードからテキストの取り出し(修正)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#クリップボードに入っているテキストを取り出す
005#
006#
007#com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use scripting additions
013
014property refMe : a reference to current application
015
016########################
017## クリップボードの中身取り出し
018#初期化
019set appPasteboard to refMe's NSPasteboard's generalPasteboard()
020#格納されているタイプをリストにして
021set ocidPastBoardTypeArray to appPasteboard's types()
022#テキストがあれば
023set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString)
024if (boolContain as boolean) is true then
025  #値を格納する
026  set ocidReadStringData to (appPasteboard's dataForType:(refMe's NSPasteboardTypeString))
027  set ocidReadString to refMe's NSString's alloc()'s initWithData:(ocidReadStringData) encoding:(refMe's NSUTF8StringEncoding)
028else
029    log "テキストなし"
030    return "クリップボードに\nテキストは\nありませんでした"
031    # set strSetSting to ("クリップボードに\nテキストは\nありませんでした") as text
032    # set ocidReadString to refMe's NSMutableString's alloc()'s initWithString:(strSetSting)
033end if
034
035
036
AppleScriptで生成しました

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#クリップボードに入っているテキストを取り出す
005#
006#
007#com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use scripting additions
013
014property refMe : a reference to current application
015
016########################
017## クリップボードの中身取り出し
018#初期化
019set appPasteboard to refMe's NSPasteboard's generalPasteboard()
020#格納されているタイプをリストにして
021set ocidPastBoardTypeArray to appPasteboard's types()
022#テキストがあれば
023set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString)
024if (boolContain as boolean) is true then
025  #値を格納する
026    set ocidTypeClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
027    ocidTypeClassArray's addObject:(refMe's NSString)
028    set ocidReadStringArray to appPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value)
029    set ocidReadString to ocidReadStringArray's firstObject()
030else
031    log "テキストなし"
032    return "クリップボードに\nテキストは\nありませんでした"
033    # set strSetSting to ("クリップボードに\nテキストは\nありませんでした") as text
034    # set ocidReadString to refMe's NSMutableString's alloc()'s initWithString:(strSetSting)
035end if
036
037
038
AppleScriptで生成しました

|

画像データをクリップボードにPDF形式で入れる


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#  com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use framework "PDFKit"
010use scripting additions
011
012property refMe : a reference to current application
013
014################################
015#ダイアログ
016set strName to (name of current application) as text
017if strName is "osascript" then
018  tell application "Finder" to activate
019else
020  tell current application to activate
021end if
022
023set appFileManager to refMe's NSFileManager's defaultManager()
024set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
025set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
026set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
027set listUTI to {"public.image"}
028set strMes to ("ファイルを選んでください") as text
029set strPrompt to ("ファイルを選んでください") as text
030try
031  ### ファイル選択時
032  set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
033on error
034  log "エラーしました"
035  return "エラーしました"
036end try
037
038################################
039#入力パス
040set strFilePath to (POSIX path of aliasFilePath) as text
041set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
042set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
043set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
044
045################################
046#NSData's
047set ocidOption to (refMe's NSDataReadingMappedIfSafe)
048set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference)
049if (item 2 of listResponse) = (missing value) then
050  log "正常処理"
051  set ocidReadData to (item 1 of listResponse)
052else if (item 2 of listResponse) ≠ (missing value) then
053  set strErrorNO to (item 2 of listDone)'s code() as text
054  set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
055  refMe's NSLog("■:" & strErrorNO & strErrorMes)
056  return "エラーしました" & strErrorNO & strErrorMes
057end if
058################################
059#NSImage's
060set ocidReadImage to refMe's NSImage's alloc()'s initWithData:(ocidReadData)
061
062################################
063#PDFPage's
064set ocidPDFPage to refMe's PDFPage's alloc()'s initWithImage:(ocidReadImage)
065
066################################
067#PDFDocument's
068set ocidPDFDoc to refMe's PDFDocument's alloc()'s init()
069ocidPDFDoc's insertPage:(ocidPDFPage) atIndex:(0)
070
071################################
072#NSData
073set ocidPDFData to ocidPDFDoc's dataRepresentation()
074
075################################
076#ペーストボード宣言
077set appPasteboard to refMe's NSPasteboard's generalPasteboard()
078appPasteboard's clearContents()
079#基本はPDF
080appPasteboard's setData:(ocidPDFData) forType:(refMe's NSPasteboardTypePDF)
081#アノテーション形式で入れれば大丈夫か?と思ったがダメだった
082#appPasteboard's setData:(ocidPDFData) forType:("com.apple.AnnotationKit.AnnotationItem")
083#基本系
084#appPasteboard's setData:(ocidPDFData) forType:("Apple PDF pasteboard type")
085#PDFのUTI指定
086#appPasteboard's setData:(ocidPDFData) forType:("com.adobe.pdf")
087#QTのクリップボードデータ(FoxIt)
088#appPasteboard's setData:(ocidPDFData) forType:("com.trolltech.anymime.FPMDATA")
089#Acrobatのスタンプデータ
090#appPasteboard's setData:(ocidPDFData) forType:("ASEL")
091
AppleScriptで生成しました

|

より以前の記事一覧

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom