Skip to content

Instantly share code, notes, and snippets.

@DrLulz
Last active August 29, 2015 14:14
Show Gist options
  • Save DrLulz/df2ea9c612aa8563cd46 to your computer and use it in GitHub Desktop.
Save DrLulz/df2ea9c612aa8563cd46 to your computer and use it in GitHub Desktop.
on run argv
set query to argv as text
tell application "Skim"
set matches to {}
set pg_hz to {}
set pg_nums to {}
set doc to front document
set doc_pages to pages of doc
repeat with p in items of doc_pages
set pg_wrds to words of text of p
set pg_num to index of p
set pg_match to my match_per_pg(query, pg_wrds, pg_num)
set end of pg_hz to pg_match
end repeat
--return pg_hz
--return my hz_sort(pg_hz)
repeat with i in pg_hz
if item 1 of i is greater than 0 then
set n to (item 2 of i)
if n is not in pg_nums then set end of pg_nums to n
tell doc to set found_text to find text query from page n
set end of matches to found_text
repeat ((item 1 of i) - 1) times
set last_char to (get index for found_text with last)
tell doc to set found_text to find text query from character last_char of page n
set end of matches to found_text
end repeat
end if
end repeat
--return matches
--return pg_nums
if pg_nums is {} then
--display dialog "The word " & query & " was not found" giving up after 2
set display to "The word " & query & " was not found" --to large type
return display
end if
select matches with animation
activate
repeat with n in pg_nums
tell doc to go to page n
set keypress to false
set time_start to do shell script "date +%s"
repeat until keypress is true
set time_exit to do shell script "date +%s"
set keypress to my isControlKeyPressed()
if time_exit - time_start > 10 then exit repeat
end repeat
if time_exit - time_start > 10 then exit repeat
end repeat
end tell
end run
on isControlKeyPressed() -- NSShiftKeyMask, NSControlKeyMask, NSAlternateKeyMask, NSCommandKeyMask
return (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSControlKeyMask > 1'") is "True"
end isControlKeyPressed
on hz_sort(pg_hz)
repeat with i from 1 to ((count of pg_hz) - 1)
repeat with j from i + 1 to count of pg_hz
if LT(item j of pg_hz, item i of pg_hz, 1) then
set {item i of pg_hz, item j of pg_hz} to {item j of pg_hz, item i of pg_hz}
end if
end repeat
end repeat
return pg_hz
end hz_sort
on match_per_pg(_query, _wrds, _page)
set the match_counter to 0
repeat with i from 1 to the count of _wrds
if item i of _wrds is _query then set the match_counter to the match_counter + 1
end repeat
return the match_counter & _page
end match_per_pg
on LT(aRay, bRay, columnToCompare)
set aTerm to item columnToCompare of aRay
set bterm to item columnToCompare of bRay
return aTerm > bterm
end LT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment