Search string in a PDF

I’d like to create a macro that would allow me to search for a selected string (a string that I select manually in my translation editor CafeTran) in an open PDF file. This file would be displayed on a second screen, though I don’t think that’s relevant here ;). Just want to mention all parameters.

It’s not crucial which viewer is used for the search (Preview, Acrobat Reader …).

The macro should allow Find again, if the first instance of the search string isn’t the one that you were looking for. It would be ideal if the found string would be highlighted in the PDF.

I would prefer a solution via scripting or so, not with send keys, so that my CafeTran doesn’t lose focus.

Did someone create a solution for this in the past or can give me a hint to set up the search?

This doesn’t work:

activate application "Adobe Reader"
tell application "System Events"
  tell process "Adobe Reader"
    click menu item "Find" of menu 1 of menu bar item "Edit" of menu bar 1
    keystroke "v" using {command down}
  end tell
end tell

The clipboard content is sent to the applescript editor :frowning:

Thanks in advance!

Hans

The Automator workflow to search PDFs cannot be used either: it doesn’t search in one PDF file that you have already opened.

Inspired by Jack's suggestion for another macro, I came up with this solution. It works, but the switching to Acrobat Reader is a little distracting:

Found a solution for syncing Word with CafeTran, AppleScript launched with Keyboard Maestro, all very elegant and swift:

http://www.proz.com/forum/cafetran_support/278770-sync_cafetran_with_word.html

Now I’m still looking for a comparable solution to search PDF files. Looks like Skim can be used for this:

find v : Find text in a document.
find document : The document in which to search.
text text : The text to search for.
[from list of specifier or specifier] : The selection to search from.
[backward search boolean] : Search backward?
[case sensitive search boolean] : Is the search case sensitive?
→ list of specifier : Selection for the found text as a list of character ranges.

Anyone, who’s familiar with scripting Skim from Keyboard Maestro? Especially with the Find text command?

Obviously, this is too simple:

tell application "Skim"
  set findText to (the clipboard as text)
  find text findText
end tell

Hey Folks,

When you post code on the board please use the ‘Prefomatted text’ item in the toolbar to get it to show up with proper indentation.

The icon looks like this: </>


Best Regards,
Chris

Hey Hans,

This will find the first instance.

set textToFind to get the clipboard
tell application "Skim"
   set foundText to find front document text textToFind
   select foundText with animation
end tell

I have not been able to find any examples of using:

from list of specifier or specifier] : The selection to search from

So I'm unsure if there's a directly scriptable method of finding the next instance. Maybe, maybe not.

From there you can set the find-pasteboard in Keyboard Maestro to the clipboard and find-next in Skim using either Keyboard Maestro or an AppleScript.

tell application "System Events"
   tell application process "Skim"
      set frontmost to true
      tell menu bar 1
         tell menu 1 of menu bar item "Edit"
            tell menu 1 of menu item "Find"
               click menu item "Find Next"
            end tell
         end tell
      end tell
   end tell
end tell

It does not appear that Skim supports UI-Scripting in the background, so either way you're going to have to activate Skim, find-text, and then return to the app you're working in.

--
Best Regards,
Chris

1 Like

Thank you once more, Chris.

Much appreciated!

Hans

Hey Chris,

I've come up with the following approach:

Find Next in PDF.kmmacros (9.9 KB)

1 Like