[Safari]前面ウィンドウの全てのタブを保存して、後でTABの内容ごと復帰させる(タブグループの名前で保存できるようにした v1.1)
ダウンロード - windowsaveandrestore.zip
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | サファリの前面Windowの内容を保存する |
005 | PLIST形式にして保存しておく tabの構成も保存される |
006 | 拡張子は独自のtabgroupを使用 |
007 | 中身はXML形式のplist |
008 | |
009 | v1 初回作成 |
010 | v1.1 ファイル名を選べるようにした |
011 | |
012 | com.cocolog-nifty.quicktimer.icefloe *) |
013 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
014 | use AppleScript version "2.8" |
015 | use framework "Foundation" |
016 | use framework "AppKit" |
017 | use scripting additions |
018 | property refMe : a reference to current application |
019 | |
020 | |
021 | tell application "Safari" |
022 | set numCntWindow to (count of every window) as integer |
023 | end tell |
024 | |
025 | if numCntWindow = 0 then |
026 | tell application "Safari" |
027 | activate |
028 | display alert "SafariでURLを開いていません" buttons {"終了"} default button "終了" cancel button "終了" as informational giving up after 3 |
029 | end tell |
030 | return "Windowがありません" |
031 | end if |
032 | #保存用のARRAY |
033 | set ocidSaveArray to refMe's NSMutableArray's alloc()'s init() |
034 | |
035 | #アプリケーション |
036 | tell application "Safari" |
037 | #前面ウィンドウ |
038 | tell front window |
039 | set numCurrentTabIndex to (index of current tab) as integer |
040 | #タブの数を数えて |
041 | set numCntTab to (count of every tab) as integer |
042 | #タブの情報を取得 |
043 | repeat with itemNo from 1 to numCntTab by 1 |
044 | tell tab itemNo |
045 | set numIndex to index as integer |
046 | set strURL to URL as text |
047 | set strTitle to name as text |
048 | set boolVisible to visible as boolean |
049 | end tell |
050 | |
051 | #URL情報をレコードにして |
052 | if numIndex = numCurrentTabIndex then |
053 | set recordTAB to {|index|:numIndex, |URL|:strURL, |name|:strTitle, |visible|:boolVisible, |activTab|:true} as record |
054 | else |
055 | set recordTAB to {|index|:numIndex, |URL|:strURL, |name|:strTitle, |visible|:boolVisible, |activTab|:false} as record |
056 | end if |
057 | |
058 | set ocidTabDict to refMe's NSMutableDictionary's alloc()'s init() |
059 | (ocidTabDict's setDictionary:(recordTAB)) |
060 | #リストに追加 |
061 | (ocidSaveArray's addObject:(ocidTabDict)) |
062 | end repeat |
063 | end tell |
064 | end tell |
065 | |
066 | #PLISTにする |
067 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
068 | set ocidOption to (refMe's NSPropertyListMutableContainersAndLeaves) |
069 | set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidSaveArray) format:(ocidFormat) options:(0) |error|:(reference) |
070 | set ocidPlistData to (item 1 of listResponse) |
071 | |
072 | |
073 | #保存先 |
074 | set appFileManager to refMe's NSFileManager's defaultManager() |
075 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
076 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
077 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Apple/Safari/SaveWindow") isDirectory:(true) |
078 | set aliasSaveDirPath to (ocidSaveDirPathURL's absoluteURL()) as alias |
079 | |
080 | #フォルダ作成 |
081 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
082 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
083 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
084 | # |
085 | tell application "Safari" |
086 | tell front window |
087 | tell current tab |
088 | set strTabName to name as text |
089 | end tell |
090 | end tell |
091 | end tell |
092 | |
093 | #日付を取得してファイル名に |
094 | set strDateNo to doGetDateNo("yyyyMMdd_HHmmss") |
095 | set strSetSaveName to ("" & strTabName & "@" & strDateNo & ".tabgroup") as text |
096 | # |
097 | set strName to (name of current application) as text |
098 | if strName is "osascript" then |
099 | tell application "Finder" to activate |
100 | else |
101 | tell current application to activate |
102 | end if |
103 | set strPromptText to "名前を決めてください" as text |
104 | set strMesText to "名前を決めてください" as text |
105 | ###ファイル名 ダイアログ |
106 | tell application "SystemUIServer" |
107 | activate |
108 | set aliasSaveFilePath to (choose file name strMesText default location aliasSaveDirPath default name strSetSaveName with prompt strPromptText) as «class furl» |
109 | end tell |
110 | tell application "SystemUIServer" to quit |
111 | ###パス |
112 | set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text |
113 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
114 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
115 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
116 | set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent() |
117 | ###拡張子 |
118 | set strExtension to (ocidSaveFilePathURL's pathExtension()) as text |
119 | ###最後のアイテムがファイル名 |
120 | set strFileName to (ocidSaveFilePathURL's lastPathComponent()) as text |
121 | ###拡張子のつけ忘れ対策 |
122 | if strFileName does not contain "tabgroup" then |
123 | set strFileName to (strFileName & ".tabgroup") as text |
124 | set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName) |
125 | end if |
126 | |
127 | |
128 | #保存 |
129 | set ocidOption to (refMe's NSDataWritingAtomic) |
130 | set listDone to ocidPlistData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference) |
131 | |
132 | |
133 | |
134 | ############################## |
135 | ### 今の日付日間 テキスト |
136 | ############################## |
137 | to doGetDateNo(argDateFormat) |
138 | ####日付情報の取得 |
139 | set ocidDate to current application's NSDate's |date|() |
140 | ###日付のフォーマットを定義 |
141 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
142 | ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX") |
143 | set ocidTimeZone to refMe's NSTimeZone's alloc()'s initWithName:"Asia/Tokyo" |
144 | ocidNSDateFormatter's setTimeZone:(ocidTimeZone) |
145 | ocidNSDateFormatter's setDateFormat:(argDateFormat) |
146 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate |
147 | set strDateAndTime to ocidDateAndTime as text |
148 | return strDateAndTime |
149 | end doGetDateNo |
AppleScriptで生成しました |
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | サファリの保存したWindowの内容をレストアする |
005 | 前面tabも復帰させるようにした |
006 | |
007 | |
008 | v1 初回作成 |
009 | v1.1 タブが1つの登録時にエラーにならないようにした |
010 | |
011 | com.cocolog-nifty.quicktimer.icefloe *) |
012 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
013 | use AppleScript version "2.8" |
014 | use framework "Foundation" |
015 | use framework "AppKit" |
016 | use scripting additions |
017 | property refMe : a reference to current application |
018 | |
019 | |
020 | #保存先 |
021 | set appFileManager to refMe's NSFileManager's defaultManager() |
022 | #書類フォルダ |
023 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
024 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
025 | #保存先ディレクトリ |
026 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Apple/Safari/SaveWindow") isDirectory:(true) |
027 | set aliasSaveDirPath to (ocidSaveDirPathURL's absoluteURL()) as alias |
028 | #コンテンツの収集 |
029 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
030 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
031 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
032 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidSaveDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error|:(reference)) |
033 | set ocidSubPathURLArray to (item 1 of listResponse) |
034 | #ファイル名のリストにする |
035 | set listChooser to {} as list |
036 | repeat with itemURL in ocidSubPathURLArray |
037 | set strFileName to itemURL's lastPathComponent() as text |
038 | set end of listChooser to strFileName |
039 | end repeat |
040 | #ダイアログ |
041 | set strName to (name of current application) as text |
042 | if strName is "osascript" then |
043 | tell application "Finder" to activate |
044 | else |
045 | tell current application to activate |
046 | end if |
047 | set strTitle to ("選んでください") as text |
048 | set strPrompt to ("選んだ項目で戻します") as text |
049 | try |
050 | set objResponse to (choose from list listChooser with title strTitle with prompt strPrompt default items (item 1 of listChooser) OK button name "OK" cancel button name "キャンセル" with empty selection allowed without multiple selections allowed) |
051 | on error |
052 | log "エラーしました" |
053 | return "エラーしました" |
054 | end try |
055 | log class of objResponse |
056 | if (class of objResponse) is boolean then |
057 | return "キャンセルしましたA" |
058 | else if (class of objResponse) is list then |
059 | if objResponse is {} then |
060 | return "キャンセルしましたB" |
061 | else |
062 | set strResponse to (item 1 of objResponse) as text |
063 | end if |
064 | end if |
065 | #選んだファイルのURL |
066 | set ocidPlistFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strResponse) isDirectory:(false) |
067 | #Plist読み込み NSData's |
068 | set ocidOption to (current application's NSDataReadingMappedIfSafe) |
069 | set listResponse to current application's NSData's alloc()'s initWithContentsOfURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference) |
070 | set ocidPlistData to (item 1 of listResponse) |
071 | #ARRAYにする |
072 | set ocidFormat to (current application's NSPropertyListXMLFormat_v1_0) |
073 | set appPlistSerial to current application's NSPropertyListSerialization |
074 | set ocidOption to current application's NSPropertyListMutableContainersAndLeaves |
075 | set listResponse to appPlistSerial's propertyListWithData:(ocidPlistData) options:(ocidOption) format:(ocidFormat) |error|:(reference) |
076 | #これがPLISTのデータArray形式 |
077 | set ocidPlistArray to (item 1 of listResponse) |
078 | #数を数えて |
079 | set numCntArray to ocidPlistArray's |count|() |
080 | #前面TABの記憶用 |
081 | set numFrontIndex to (missing value) |
082 | #その数だけ繰り返し |
083 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
084 | set itemDict to (ocidPlistArray's objectAtIndex:(itemNo)) |
085 | set strSetURL to (itemDict's valueForKey:("URL")) as text |
086 | #最初だけ新規WINDOW |
087 | if itemNo = 0 then |
088 | tell application "Safari" |
089 | make new document with properties {URL:strSetURL} |
090 | end tell |
091 | else |
092 | #それ以外は新規tab |
093 | tell application "Safari" |
094 | tell front window |
095 | set objTab to make new tab at end of tabs with properties {URL:strSetURL} |
096 | end tell |
097 | end tell |
098 | end if |
099 | #前面タブの判定 |
100 | set boolFrontMost to (itemDict's valueForKey:("visible")) as boolean |
101 | if boolFrontMost is true then |
102 | #前面にするタブを格納 |
103 | set refTab to objTab |
104 | end if |
105 | end repeat |
106 | tell application "Safari" |
107 | tell front window |
108 | set numCntTab to (count of every tab) as integer |
109 | end tell |
110 | end tell |
111 | if numCntTab > 1 then |
112 | |
113 | tell application "Safari" |
114 | tell front window |
115 | set current tab to refTab |
116 | end tell |
117 | end tell |
118 | end if |
AppleScriptで生成しました |