Safariで開いているページの中から、特定のページを自動的に閉じる。
ざっくり作ったので改良が必要かもしれませんが、
任意で登録したWebアドレスと同じページが開いたら、自動的に閉じるというものを作ってみました。
いくつかのWebページがあります。
今回の一例として使いましたが、Wikiのページ(悪いページではないのですが)このページが開いていたら強制的に閉じるようにします。
プログラムを走らせると、まずは何も登録されていないので、どのWebページも閉じられることがありません。
一番上のテーブルには現在までに開かれたページアドレス と、開かれたと認識された時間 のリストが表示されます。
試しに、Wikiのアドレスをリストの中から選び右の"Kick"ボタンをクリックします。
登録されたアドレスが2段目に表示され、同時にWebページが閉じられました。
ここで登録されたページは何度開いても、即座に閉じられます。
閉じられたページのログは一番下のリストに、その都度、表示されていきます。
登録されたページを外したい場合は2段目のリストからアドレスを選び、"Delete"ボタンを押すと解除されます。この後、そのページを開いても閉じられることはありません。
(今後の課題)
大きくて邪魔なので縮小化できるようにしてみたい。
(* AppleEventsを使用するため、info.plistに『Privacy - AppleEvents Sending Usage Description』を追加してください *) (* ユーザーフォルダ内のドキュメントフォルダの中にDatabaseフォルダが作成されていますので、不要になりましたら削除してください。 *) script AppDelegate property parent : class "NSObject" property theWindow : missing value
--Timer global updateTimer
--SplitView global splitView1, splitViewItem1, splitViewItem2, splitViewItem3 --TableView property theDataSource1 : {} global aTableView1, sourceList1 property theDataSource2 : {} global aTableView2, sourceList2 property theDataSource3 : {} global aTableView3, sourceList3
--autoresizing global autoresizingViewFree
--data global currentFolder global hanteiList, activateData, killUrl global oldPageList
on awakeFromNib() theWindow's setFrameAutosaveName_("mainWindow") end awakeFromNib
on applicationWillFinishLaunching_(aNotification) theWindow's setMinSize_(current application's NSMakeSize(420,300)) set {{window_x,window_y},{window_w,window_h}} to theWindow's contentView()'s frame() (* autoresizing *) set autoresizingViewFree to ((current application's NSViewWidthSizable) as integer) + ((current application's NSViewHeightSizable) as integer) (* SplitView *) --NSSplitView用アイテムを複数個作成 set theRect to current application's NSMakeRect(0,0,window_w,window_h) set splitViewItem1 to current application's class "NSView"'s alloc()'s init() set splitViewItem2 to current application's class "NSView"'s alloc()'s init() set splitViewItem3 to current application's class "NSView"'s alloc()'s init() --NSSplitView作成 set aVertical to false set aDividerStyle to current application's NSSplitViewDividerStylePaneSplitter set splitView1 to current application's class "NSSplitView"'s alloc()'s initWithFrame:theRect tell splitView1 setVertical_(aVertical) --|方向:true=左右, false=上下| setDividerStyle_(aDividerStyle) --|仕切りのタイプ:1=厚, 2=薄, 3=3D| addArrangedSubview_(splitViewItem1) addArrangedSubview_(splitViewItem2) addArrangedSubview_(splitViewItem3) setAutosaveName_("splitView_1") end tell theWindow's contentView()'s addSubview:splitView1 splitView1's setAutoresizingMask_(autoresizingViewFree)
set {{splitView1_X, splitView1_Y}, {splitView1_W, splitView1_H}} to splitView1's frame() set viewItemCount to count of (splitView1's subviews()) set aDividerThickness to (splitView1's dividerThickness()) * (viewItemCount - 1) set splitView1_H to (splitView1_H - aDividerThickness) / viewItemCount splitViewItem1's setFrameSize:{splitView1_W, splitView1_H} splitViewItem2's setFrameSize:{splitView1_W, splitView1_H} splitViewItem3's setFrameSize:{splitView1_W, splitView1_H} splitView1's setAutoresizingMask_(autoresizingViewFree)
(* TableView *) -- ●●●●● NSTableView 1 set {table_w,table_h} to item 2 of ((splitViewItem1's frame) as list) set table_w to table_w - 80 set sourceList1 to {} --{{data1:1},{data1:2},{data1:3}} set theDataSource1 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource1's addObjectsFromArray_(sourceList1) set aColumn1 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier_("data1") tell aColumn1 headerCell()'s setTitle_("Geting") setWidth_(table_w - 3) setEditable_(false) end tell set aTableView1 to current application's class "NSTableView"'s alloc()'s initWithFrame_(current application's NSMakeRect(0, 0, table_w,table_h)) tell aTableView1 addTableColumn_(aColumn1) setDelegate_(me) setDataSource_(me) setUsesAlternatingRowBackgroundColors_(true) setAllowsMultipleSelection_(true) end tell set aScroll1 to current application's class "NSScrollView"'s alloc()'s initWithFrame_(current application's NSMakeRect(0,0,table_w,table_h)) aScroll1's setDocumentView_(aTableView1) splitViewItem1's addSubview_(aScroll1) tell aScroll1 setHasHorizontalScroller_(true) setHasVerticalScroller_(true) setAutohidesScrollers_(true) setAutoresizingMask_(autoresizingViewFree) end tell aTableView1's reloadData()
-- ●●●●● NSTableView 2 set {table_w,table_h} to item 2 of ((splitViewItem2's frame) as list) set table_w to table_w - 80 set sourceList2 to {} --{{data2:4},{data2:5},{data2:6}} set theDataSource2 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource2's addObjectsFromArray_(sourceList2) set aColumn2 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier_("data2") tell aColumn2 headerCell()'s setTitle_("killing url") setWidth_(table_w - 3) setEditable_(false) end tell set aTableView2 to current application's class "NSTableView"'s alloc()'s initWithFrame_(current application's NSMakeRect(0, 0, table_w,table_h)) tell aTableView2 addTableColumn_(aColumn2) setDelegate_(me) setDataSource_(me) setUsesAlternatingRowBackgroundColors_(true) setAllowsMultipleSelection_(true) end tell set aScroll2 to current application's class "NSScrollView"'s alloc()'s initWithFrame_(current application's NSMakeRect(0,0,table_w,table_h)) aScroll2's setDocumentView_(aTableView2) splitViewItem2's addSubview_(aScroll2) tell aScroll2 setHasHorizontalScroller_(true) setHasVerticalScroller_(true) setAutohidesScrollers_(true) setAutoresizingMask_(autoresizingViewFree) end tell aTableView2's reloadData()
-- ●●●●● NSTableView 3 set {table_w,table_h} to item 2 of ((splitViewItem3's frame) as list) set table_w to table_w - 80 set sourceList3 to {} --{{data3:7},{data3:8},{data3:9}} set theDataSource3 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource3's addObjectsFromArray_(sourceList3) set aColumn3 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier_("data3") tell aColumn3 headerCell()'s setTitle_("Kill Log") setWidth_(table_w - 3) setEditable_(false) end tell set aTableView3 to current application's class "NSTableView"'s alloc()'s initWithFrame_(current application's NSMakeRect(0, 0, table_w,table_h)) tell aTableView3 addTableColumn_(aColumn3) setDelegate_(me) setDataSource_(me) setUsesAlternatingRowBackgroundColors_(true) setAllowsMultipleSelection_(true) end tell set aScroll3 to current application's class "NSScrollView"'s alloc()'s initWithFrame_(current application's NSMakeRect(0,0,table_w,table_h)) aScroll3's setDocumentView_(aTableView3) splitViewItem3's addSubview_(aScroll3) tell aScroll3 setHasHorizontalScroller_(true) setHasVerticalScroller_(true) setAutohidesScrollers_(true) setAutoresizingMask_(autoresizingViewFree) end tell aTableView3's reloadData()
(* button *) set autoresizingViewMinXMargin to ((current application's NSViewMinXMargin) as integer) + ((current application's NSViewMinYMargin) as integer) --左と下を柔軟に --button1 set {w,h} to item 2 of ((splitViewItem1's frame) as list) set {x,y} to {w - 78,h - 50} set theRect to current application's NSMakeRect(x,y,75,24) set button1 to current application's class "NSButton"'s alloc()'s initWithFrame_(theRect) tell button1 setBezelStyle_(1) setButtonType_(0) setTitle_("Kick") setTarget_(me) setAction_("action1:") setAutoresizingMask_(autoresizingViewMinXMargin) end tell splitViewItem1's addSubview_(button1) --button2 set {w,h} to item 2 of ((splitViewItem2's frame) as list) set {x,y} to {w - 78,h - 50} set theRect to current application's NSMakeRect(x,y,75,24) set button2 to current application's class "NSButton"'s alloc()'s initWithFrame_(theRect) tell button2 setBezelStyle_(1) setButtonType_(0) setTitle_("Delete") setTarget_(me) setAction_("action2:") setAutoresizingMask_(autoresizingViewMinXMargin) end tell splitViewItem2's addSubview_(button2) --button3 set {w,h} to item 2 of ((splitViewItem3's frame) as list) set {x,y} to {w - 78,h - 50} set theRect to current application's NSMakeRect(x,y,75,24) set button3 to current application's class "NSButton"'s alloc()'s initWithFrame_(theRect) tell button3 setBezelStyle_(1) setButtonType_(0) setTitle_("Clear") setTarget_(me) setAction_("action3:") setAutoresizingMask_(autoresizingViewMinXMargin) end tell splitViewItem3's addSubview_(button3)
--database set dataFolderName to "killWebPage" set currentFolder to my makeDatabaseFolder(dataFolderName) --data set hanteiList to "" set activateData to "" try set killUrl to do shell script ("cat '" & currentFolder & "killList.txt' ;") repeat with oneLine in (paragraphs of killUrl) set sourceList2 to sourceList2 & {{data2:(oneLine as text)}} end repeat set theDataSource2 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource2's addObjectsFromArray_(sourceList2) aTableView2's reloadData() on error set killUrl to "" end try try do shell script ("rm '" & currentFolder & "deletePageLog.txt' ;") end try
--TimerStart set oldPageList to "" set updateTimer to current application's class "NSTimer"'s scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1.0, me, "selector1:", missing value, true) end applicationWillFinishLaunching_
on action1_(sender) set theSelection to aTableView1's selectedRowIndexes() set aList to {} set selectionCount to (theSelection's |count|) as integer if selectionCount = 0 then log "count : 0" return else set data1 to theSelection's firstIndex() set data2 to theSelection's lastIndex() set nextIndex to 0 repeat selectionCount times set data3 to theSelection's indexGreaterThanOrEqualToIndex_(nextIndex) set aList to aList & {data3} set nextIndex to data3 + 1 end repeat end if --log aList repeat with aIndex in aList set aUrl to text 11 thru -1 of ((paragraph (aIndex + 1) of activateData) as text) log aUrl -- if not (killUrl contains "/") then set killUrl to aUrl else set killUrl to killUrl & return & aUrl end if set sourceList2 to {} repeat with oneLine in (paragraphs of killUrl) if oneLine contains "/" then set sourceList2 to sourceList2 & {{data2:(oneLine as text)}} end repeat -- --hanteiList set hanteiList to " " & return & hanteiList & return & " " set data1 to text from paragraph 1 to paragraph (aIndex + 1) of hanteiList set data2 to text from paragraph (aIndex + 3) to paragraph -1 of hanteiList set hanteiList to text from paragraph 2 to paragraph -2 of (data1 & return & data2) if not (hanteiList contains "/") then set hanteiList to "" --activateData set activateData to " " & return & activateData & return & " " set data1 to text from paragraph 1 to paragraph (aIndex + 1) of activateData set data2 to text from paragraph (aIndex + 3) to paragraph -1 of activateData set activateData to text from paragraph 2 to paragraph -2 of (data1 & return & data2) if not (activateData contains "/") then set activateData to "" end repeat set killUrl2 to "" repeat with aUrl in (paragraphs of killUrl) set aUrl to aUrl as text if aUrl contains "/" then set killUrl2 to killUrl2 & aUrl & return end repeat set killUrl to text from paragraph 1 to paragraph -2 of killUrl2 do shell script ("echo \"" & killUrl & "\" > '" & currentFolder & "killList.txt' ;") -- set theDataSource2 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource2's addObjectsFromArray_(sourceList2) aTableView2's reloadData() -- my killUrlPage() -- set sourceList1 to {} repeat with oneLine in (paragraphs of activateData) if (oneLine as text) contains "/" then set sourceList1 to sourceList1 & {{data1:oneLine}} end repeat set theDataSource1 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource1's addObjectsFromArray_(sourceList1) aTableView1's reloadData() -- my activateUrlList() end action1_
on action2_(sender) set theSelection to aTableView2's selectedRowIndexes() set aList to {} set selectionCount to (theSelection's |count|) as integer if selectionCount = 0 then log "count : 0" return else set data1 to theSelection's firstIndex() set data2 to theSelection's lastIndex() set nextIndex to 0 repeat selectionCount times set data3 to theSelection's indexGreaterThanOrEqualToIndex_(nextIndex) set aList to aList & {data3} set nextIndex to data3 + 1 end repeat end if set aData to " " & return & killUrl & return & " " repeat with aIndex in aList set aData1 to text from paragraph 1 to paragraph (aIndex + 1) of aData set aData2 to text from paragraph (aIndex + 3) to paragraph -1 of aData end repeat set killUrl to text from paragraph 2 to paragraph -2 of (aData1 & return & aData) if not (killUrl contains "/") then set killUrl to "" else set killUrl2 to "" repeat with oneLine in (paragraphs of killUrl) set oneLine to oneLine as text if oneLine contains "/" then set killUrl2 to killUrl & oneLine & return end repeat set killUrl to text from paragraph 1 to paragraph -2 of killUrl end if -- do shell script ("echo \"" & killUrl & "\" > '" & currentFolder & "killList.txt' ;") -- set sourceList2 to {} repeat with oneLine in (paragraphs of killUrl) if oneLine contains "/" then set sourceList2 to sourceList2 & {{data2:(oneLine as text)}} end repeat set theDataSource2 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource2's addObjectsFromArray_(sourceList2) aTableView2's reloadData() end action2_
on action3_(sender) try do shell script ("rm '" & currentFolder & "deletePageLog.txt' ;") end try set theDataSource3 to current application's class "NSMutableArray"'s alloc()'s init() aTableView3's reloadData() end action3_
on killUrlPage() tell application "Safari" repeat with aWindow in windows tell aWindow repeat with aTabNo from (count of tabs) to 1 by -1 set aTab to tab aTabNo set aUrl1 to (url of aTab) as text if not (aUrl1 contains "/") then set aUrl1 to "" if aUrl1 /= "" then repeat with aUrl2 in (paragraphs of killUrl) if aUrl2 /= "" then if aUrl1 contains aUrl2 then close aTab set aDateText to time string of (current date) set aLogText to aDateText & " " & aUrl1 & " (Delete)" do shell script ("echo \"" & aLogText & "\" >> '" & currentFolder & "deletePageLog.txt' ;") exit repeat end if end if end repeat end if end repeat end tell end repeat end tell
try set aSource to do shell script ("cat '" & currentFolder & "deletePageLog.txt' ;") set sourceList3 to {} repeat with oneLine in (paragraphs of aSource) set oneLine to oneLine as text if oneLine contains "/" then set sourceList3 to sourceList3 & {{data3:oneLine}} end repeat set theDataSource3 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource3's addObjectsFromArray_(sourceList3) aTableView3's reloadData() end try end killUrlPage
on activateUrlList() set aTime to time string of (current date) tell application "Safari" repeat with aWindow in windows tell aWindow repeat with aTabNo from (count of tabs) to 1 by -1 set aUrl to URL of tab aTabNo if aUrl contains "/" then if not (hanteiList contains ("@" & aUrl & "@")) then set activateData to activateData & aTime & " " & aUrl & return set hanteiList to hanteiList & "@" & aUrl & "@" & return end if end if end repeat end tell end repeat end tell if activateData /= oldPageList then set sourceList1 to {} repeat with oneLine in (paragraphs of activateData) if (oneLine as text) contains "/" then set sourceList1 to sourceList1 & {{data1:oneLine}} end repeat set theDataSource1 to current application's class "NSMutableArray"'s alloc()'s init() theDataSource1's addObjectsFromArray_(sourceList1) aTableView1's reloadData() end if end activateUrlList
on selector1_(sender) my killUrlPage() my activateUrlList() end selector1
(* TableViewの表示行数処理(必須) *) on numberOfRowsInTableView_(targetTableView) try if targetTableView = aTableView1 then set c to count of (my theDataSource1) else if targetTableView = aTableView2 then set c to count of (my theDataSource2) else if targetTableView = aTableView3 then set c to count of (my theDataSource3) end if on error set c to 0 end try return c end numberOfRowsInTableView_
(* TableViewデータの処理(必須) *) on tableView_objectValueForTableColumn_row_(targetTableView, aColumn, aRow) try if targetTableView = aTableView1 then set aRec to (my theDataSource1)'s objectAtIndex_(aRow as number) else if targetTableView = aTableView2 then set aRec to (my theDataSource2)'s objectAtIndex_(aRow as number) else if targetTableView = aTableView3 then set aRec to (my theDataSource3)'s objectAtIndex_(aRow as number) end if set aIdentifier to (aColumn's identifier()) as string set aRes to (aRec's valueForKey_(aIdentifier)) on error set aRes to {} end try
return aRes end tableView_objectValueForTableColumn_row_
on makeDatabaseFolder(dataFolderName) set currentDirectory to do shell script "echo $HOME/Documents/" try do shell script ("cd \"" & currentDirectory & "\" ; ls Database/" & dataFolderName & " ;") return ((currentDirectory & "Database/" & dataFolderName & "/") as string) on error do shell script ("cd \"" & currentDirectory & "\" ; mkdir -m +w -p Database/" & dataFolderName & " ;") end try try do shell script ("cd \"" & currentDirectory & "\" ; ls Database/" & dataFolderName & " ;") on error try display dialog ("データベースフォルダを作成するための設定を有効にしてください。" & return & "(作成フォルダ \"~/Documents/Database/\")") buttons "Ok." end try quit me end try return ((currentDirectory & "Database/" & dataFolderName & "/") as string) end makeDatabaseFolder
on applicationShouldTerminateAfterLastWindowClosed_(sender) try tell updateTimer to invalidate() end try return true end applicationShouldTerminateAfterLastWindowClosed_ on applicationShouldTerminate_(sender) return current application's NSTerminateNow end applicationShouldTerminate_ end script |