When I fire up hook using ^H with Firefox focused, it looks like applescript is selecting the location bar and copying the value, but not returning the focus to where it was in the main frame.
I use vimium to navigate web pages, and it doesn’t work when the page itself isn’t focus. This breaks the flow of what I was doing, as I have to mess around to get back to the web content.
is it possible to return the focus where it started when collecting the url?
Hook has to use UI scripting to get the current url from Firefox. So there are some problems.
Here is a modified script. It seems to be working fine on my machine. Could you please open Hook preferences window → Script → Firefox → Get Address, replace the Get Address script with the following script?
use framework "AppKit"
-- classes, constants, and enums used
property NSShiftKeyMask : a reference to 131072
property NSAlternateKeyMask : a reference to 524288
property NSControlKeyMask : a reference to 262144
property NSEvent : a reference to current application's NSEvent
set modifier_down to true
repeat while modifier_down
set modifier_flags to NSEvent's modifierFlags()
set option_down to ((modifier_flags div (get NSAlternateKeyMask)) mod 2) = 1
set shift_down to ((modifier_flags div (get NSShiftKeyMask)) mod 2) = 1
set control_down to ((modifier_flags div (get NSControlKeyMask)) mod 2) = 1
set modifier_down to option_down or shift_down or control_down
end repeat
tell application "Firefox"
activate
delay 0.05
set myName to get name of front window
if version > 72 and myName ends with " - Mozilla Firefox" then
if length of myName > 18 then
set myName to text 1 through -19 of myName
else
set myName to ""
end if
end if
end tell
tell application "System Events"
keystroke "l" using {command down}
keystroke (key code 53)
keystroke (key code 53)
--keystroke "c" using {command down}
tell process "Firefox"
click menu item "Copy" of menu "Edit" of menu bar 1
end tell
delay 0.05
set theClipboard to current application's NSPasteboard's generalPasteboard's stringForType:(current application's NSPasteboardTypeString)
keystroke (key code 53)
keystroke (key code 48)
return ("[" & myName & "](" & the theClipboard as string) & ")"
end tell
That does not work - it leaves the cursor focused on the “reading mode” toggle. Given that you’re just adding key presses, you’ll probably have better luck with F6 (keycode 97).
I tested it with F6, and it does work. That being said… there has to be a better way of automating this. I’ve got a few other apps that observe what I’m doing, like Timing, and it can get a surprising amount of data passively, without controlling and interacting with the application.