Tracking visited websites using Webbla

Webbla is an app that shows websites as thumbnails, which is handy when searching your surfing history.
However, it does not sync automatically with Safari (although the documentation mentions user scripts)
My script syncs Webbla with Safari’s history of visited websites using cmd-shift-ctrl S, a global shortcut defined in Webbla.
However, I am running the script any 5 seconds, which is not very elegant.
Is there a way to trigger execution by detecting a new page loaded in Safari ?


set LastURL to ""
tell application "System Events" to set procs to name of the processes

repeat until "Safari" is not in procs
	tell application "System Events"
		set fApp to name of some application process whose frontmost is true
		set procs to name of the processes
	end tell
	if "Safari" is in fApp and "Webbla" is in procs then
		tell application "Safari"
			set url_current to URL of front document
		end tell
		if url_current ≠ LastURL then
			tell application "System Events" to keystroke "S" using {command down, shift down, control down}
		end if
		set LastURL to url_current
	end if
	delay 5
end repeat

Hi Eelco,

you could do:

every time after a new page is loaded, the file ~/Library/Safari/HistoryIndex.sk changes.
You could watch the folder ~/Library/Safari/ with launchd, compare the history file with the last change
and run your script if necessary

The launchd agent can look like this, you have just to adjust the paths
The agent must be located in ~/Library/LaunchAgents/, the current name is WatchingSafariFolder.plist,
if you change the name, change also the value of the key Label (without .plist extension)

the script can be saved everywhere, all paths must be absolute, space character must not be escaped.
After proper installing activate the agent in Terminal with

launchctl load -w ~/Library/LaunchAgents/WatchingSafariFolder.plist

[code]<?xml version="1.0" encoding="UTF-8"?>

Label WatchingSafariFolder LowPriorityIO Program /usr/bin/osascript ProgramArguments osascript /Users/myUser/Library/Scripts/launchd/WatchingSafariFolder.scpt ServiceDescription AppleScript directly when Volumes changes WatchPaths /Users/myUser/Library/Safari/ [/code]

Stefan,

Sounds pretty clever.
The second code is the Agent, I assume, but in what editor can I put that and how to compile it…?
Then, from a more theoretical point of view, would launchd take less overhead than my any-5-second AS routine…?

Eelco

PS:

I just wrote a script to load and unload launchd agents easily with AppleScript.
(The script assumes that the folder ~/Library/LaunchAgents exists)


set allAgents to paragraphs of (do shell script "ls ~/Library/LaunchAgents/")
set theAgent to (choose from list allAgents) as Unicode text
if theAgent is "false" then return

set launchAgentsFolder to POSIX path of ((path to library folder from user domain as text) & "LaunchAgents:")

do shell script "launchctl list "
if result contains text 1 thru -7 of theAgent then
	set prompt to theAgent & " loaded"
	set defaultButton to 2
else
	set prompt to theAgent & " not loaded"
	set defaultButton to 3
end if
set _mode to button returned of (display dialog prompt buttons {"Cancel", "unload", "load"} default button defaultButton)
try
	do shell script "launchctl " & _mode & " -w " & quoted form of (launchAgentsFolder & theAgent)
	display dialog theAgent & " successfully " & _mode & "ed." buttons {"OK"} default button 1
on error e
	display dialog "error " & e & " occured" buttons {"Cancel"} default button 1 with icon stop
end try


If you have installed the developer tools, Property List Editor is the best tool,
but actually you can take any text editor, save the code as plain text file UTF-8 encoded.

launchd takes no overhead at all, the script will be triggered only if the contents of the Safari folder has changed.
there is no folder polling, launchd uses a notification technology

Thanks, Stefan.
I tried to load the plist with your script and it registered as loaded.
Unfortunately, something goes wrong, as WatchingSafariFolder.scpt does not work.

To debug, I changed the WatchingSafariFolder.scpt to beep twice but nothing happens.

Also, when unloading I get the error:
“error launchctl: propertyList is NULL
launchctl: no plist was returned for: /Users/ehouwink/Library/LaunchAgents/WatchingSafariFolder.plist
launchctl: no plist was returned for: /Users/ehouwink/Library/LaunchAgents/WatchingSafariFolder.plist
nothing found to unload occured”

What can be wrong ?

That means, there is no WatchingSafariFolder.plist file in the LaunchAgents folder

But there is, because the “unload” buttons was hilited…!

Stefan,

The problem was that TextEdit didn’t produce the right .plist format, so the code got loaded but wasn’t recognized (therefore couldn’t be unloaded)
I solved this (not having DevTools) with PlistEdit demo which immediately recognized your code.

Now working like charm, incl. loading/unloading…
Thanks for your assistance / sophistication !

Good to hear that it finally works.

TextEdit should produce the right code, if the format is plain text and the encoding utf-8