AutoHotKey_Lで、Operaの起動を便利にしてみた。 ver.2.3.0
AutoHotKey_Lで、Operaの起動を便利にしてみた。をちょっと強化。AutoSave.winをブックマークに変換する際に、スタックごとにフォルダで分ける機能の追加。あと小規模改善。
; --------- --------- --------- --------- ---------
; RunOpera.ahk ver.2.3.0
; --------- --------- --------- --------- ---------
#WinActivateForce
OperaPath := "R:\Opera" ; Opera.exeの存在するパス。末尾"\"は不可。
if %0% ; 引数があった場合、Operaのパスと見なす
OperaPath = %1%
SessionsPath := OperaPath "\profile\sessions" ; セッションフォルダのフルパスを指定。末尾"\"は不可。
BookmarksPath := OperaPath "\profile\bookmarks.adr" ; bookmarks.adrのフルパスを指定。
TimeLimit := 30 ; 最終セッション退避確認ダイアログを自動でスキップするまでの秒数。
AutoConvSW := 1 ; 最終セッションを退避させる場合、自動でブックマークに変換する設定。 0:変換しない。 1:変換する。
ACMoveSW := 1 ; 自動変換(↑)したセッションを隔離する設定。 0:隔離しない。 1:隔離する。
StackSW := 1 ; スタックの設定。 0:無視する。 1:スタックごとにフォルダを分ける。
HisReadSW := 0 ; タブ履歴(タブで表示したページの履歴)の設定。 0:最新のみ読み込む。 1:すべて読み込む。
His0PrefixSW := 0 ; タブ履歴がなかった場合のプレフィックスの設定。 0:なし。 1:0を明示する。
WriteTrgSW := 1 ; ブックマーク情報を書き込む場所の設定。 0:セッションフォルダにテキストで保存。 1:bookmarks.adrに書き込む。
; --------- --------- --------- --------- ---------
IfNotExist, %SessionsPath%\
{
MsgBox, 16, , セッションフォルダが見つかりません。`n%SessionsPath%
ExitApp
}
If WriteTrgSW {
IfNotExist, %BookmarksPath%
{
MsgBox, 16, , bookmarks.adrが見つかりません。`n%BookmarksPath%
ExitApp
}
}
IfNotExist, %SessionsPath%\autosave.win
{
IfExist, %SessionsPath%\autosave.win.bak
FileMove, %SessionsPath%\autosave.win.bak, %SessionsPath%\autosave.win
}
IfExist, %SessionsPath%\autosave.win
{
FileCopy, %SessionsPath%\autosave_RunOpBak.win, %SessionsPath%\autosave_RunOpBak2.win, 1 ; ↓セッション退避etc.を実行しない時も念のため2世代バックアップ。
FileCopy, %SessionsPath%\autosave.win, %SessionsPath%\autosave_RunOpBak.win, 1
GoSub, SessionAnalyze
}
FileCopy, %OperaPath%\ui\fastforward_backup.ini, %OperaPath%\ui\fastforward.ini, 1 ; アップデートのたびにFastForward.iniが初期化される問題の対策。
Run, %OperaPath%\opera.exe, , , OpPid
WinWait, ahk_pid %OpPid%, , 60
if !ErrorLevel {
ifWinExist, Opera へようこそ ahk_pid %OpPid%
{
WinActivate
WinWaitClose
}
} else {
return
}
WinWait, マスターパスワードを入力してください ahk_pid %OpPid%, , 5
If ErrorLevel
return
WinSet, Topmost, ON
WinActivate
Sleep, 300
ExitApp
; --------- --------- --------- --------- ---------
SessionAnalyze:
FileRead, OSession, *P65001 *t %SessionsPath%\autosave.win
If ErrorLevel {
MsgBox, 48, , Sessionの読み込みに失敗しました。`n%SessionsPath%\autosave.win
return
}
Idx := 0
WinTitles := ""
Loop {
Idx := RegExMatch(OSession, "\[(\d+)history url\][\s\S]+?count=(\d+)", $, Idx + 1)
If !Idx
Break
WinNum := $1
HisNum := $2 - 1
HisTitle := GetHisTitle(OSession, WinNum, HisNum, Idx)
WinTitles .= " " HisTitle "`n"
}
If !WinTitles
return
SetTimer, RenewCount, 1000
MsgBox, 259, 退避確認(30秒後に通常起動), 起動時に最終セッションを退避しますか?`n`n◆最終セッションの内容◆`n%WinTitles%, %TimeLimit%
SetTimer, RenewCount, Off
IfMsgBox, Yes
{
TimeNum := A_Now
BackupName := TimeNum ".win"
Loop {
FileMove, %SessionsPath%\autosave.win, %SessionsPath%\%BackupName%
If !ErrorLevel ; 最終セッションのリネームに成功なら
Break
BackupName := TimeNum "_" A_Index ".win"
}
FileDelete, %SessionsPath%\autosave.win.bak
If AutoConvSW
GoSub, SessionConvert
}
IfMsgBox, Cancel
ExitApp
return
; --------- --------- --------- --------- ---------
RenewCount:
IfWinExist, 退避確認( ahk_class #32770
{
TimeLimit--
WinSetTitle, 退避確認(%TimeLimit%秒後に通常起動)
if !TimeLimit
SetTimer, RenewCount, Off
}
return
; --------- --------- --------- --------- ---------
SessionConvert:
If WriteTrgSW
FileCopy, %BookmarksPath%, %BookmarksPath%_%A_Now% ; bookmarks.adrのバックアップは無制限なので注意
CmdLine := SessionsPath "\" BackupName
SplitPath, CmdLine, , , , SessionName
Result := "#FOLDER`n NAME=[Session] " SessionName "`n`n"
ResultBuf := Result
Idx := 0
BefGroupNum := 0
His0Prefix :=
Loop {
Idx := RegExMatch(OSession, "\[(\d+)\][\s\S]+?group=(\d+)[\s\S]+?\[\1history url\][\s\S]+?count=(\d+)", $, Idx + 1)
If !Idx
Break
WinNum := $1
GroupNum := $2
HisNum := $3 - 1
HisTitle := GetHisTitle(OSession, WinNum, HisNum, Idx)
HisUrl := GetHisUrl(OSession, WinNum, HisNum, Idx)
if StackSW
if (BefGroupNum != GroupNum) {
if BefGroupNum
Result .= "-`n`n"
if GroupNum
Result .= "#FOLDER`n NAME=[Stack] " GroupNum "`n`n"
BefGroupNum := GroupNum
}
If !HisReadSW { ; タブ履歴を無視する場合
Result .= "#URL`n NAME=" HisTitle "`n URL=" HisUrl "`n`n"
Continue
}
StringLen, HisNumLen, HisNum
If !HisNum {
if His0PrefixSW
His0Prefix := "[" ZeroSupply(HisNum, HisNumLen) "] "
Result .= "#URL`n NAME=" HisPrefix HisTitle "`n URL=" HisUrl "`n`n"
Continue
}
Result .= "#FOLDER`n NAME=" HisTitle "`n`n#URL`n NAME=[" ZeroSupply(HisNum, HisNumLen) "] " HisTitle "`n URL=" HisUrl "`n`n"
Loop, %HisNum% {
HisNum--
HisTitle := GetHisTitle(OSession, WinNum, HisNum, Idx)
HisUrl := GetHisUrl(OSession, WinNum, HisNum, Idx)
Result .= "#URL`n NAME=[" ZeroSupply(HisNum, HisNumLen) "] " HisTitle "`n URL=" HisUrl "`n`n"
}
Result .= "-`n`n"
}
If (ResultBuf == Result)
return
Result .= "-`n`n"
If WriteTrgSW {
FileRead, BMBuf, *t *P65001 %BookmarksPath%
StringGetPos, Idx, BMBuf, #DELETED
if !Idx {
StringMid, BMBufP, BMBuf, 1, Idx
StringMid, BMBufS, BMBuf, Idx + 1
Result := BMBufP Result BMBufS
FileDelete, %BookmarksPath%
}
FileAppend, %Result%, %BookmarksPath%, CP65001
} Else {
FileDelete, %CmdLine%.txt
FileAppend, %Result%, %CmdLine%.txt, CP65001
}
If ACMoveSW {
IfNotExist, %SessionsPath%\AutoConv\
FileCreateDir, %SessionsPath%\AutoConv\
FileMove, %CmdLine%, %SessionsPath%\AutoConv\%BackupName%
If ErrorLevel
MsgBox, 48, , 変換済みセッションの移動に失敗しました。`n%SessionsPath%\AutoConv\
}
return
; --------- --------- --------- --------- ---------
GetHisTitle(OSession, WinNum, HisNum, Idx){
RegExMatch(OSession, "\[" WinNum "history title\][\s\S]+?" HisNum "=(.*?)\r?\n", $)
return $1
}
GetHisUrl(OSession, WinNum, HisNum, Idx){
RegExMatch(OSession, "\[" WinNum "history url\][\s\S]+?" HisNum "=(.*?)\r?\n", $)
return $1
}
ZeroSupply(Num, NumDigit){
StringLen, NumLen, Num
LoopNum := NumDigit - NumLen
Loop, %LoopNum%
Num := 0 Num
return, Num
}
セッション確認ダイアログ
ブックマーク変換例。