Hello all,
I’m new to KM and scripting and wondering if I can get suggestions or feedback. Some background info:
I’m working with research articles (in .pdf format) and have a list of the articles in excel cloumn A (the list is a list of citations of the articles all in column A with different citations in different rows). Columns B, C, D and E I have labeled as different “keywords” that need to be found in the articles. Normally, I go through the articles and manually search each article with each keyword, switching back to excel to mark if the keyword is present or not (marking the keyword in the corresponding column B, C, D or E for the corresponding citation in column A).
I would like to automate the process for each of the files in a specific folder (all .pdf files). Here is what I have so far:
-
I start the KM Macro by prompting the user to enter a keyword, which I set as a variable (KeyWord) and have KM copy this variable to clipboard.
-
Next, I wrote an Applescript that searches a pdf with a keyword and outputs the page number(s) on which the keyword was found. If the word is not present, the script adds “NONE FOUND” to the clipboard (see script at the end of this post). I added an action in KM to run the script, in which I use the output of the system clipboard as the input into the search box for the keyword search in pdf.
-
I then have KM save the output (page numbers) in a new keyboard (named PageNumbers)
The macro works perfectly and does everything I need (for one selected file), but I’m having trouble integrating it into an actual automation. Here are the ideas/questions I would like to implement and would really appreciate any feedback (on this or the approach in general):
-
If the column A of the excel sheet I have would match the name of the pdf files being processed, is there a way for me to copy the name of the file to a new clipboard for each file being processed? I’m thinking if I’m able to do this, I can create an action that will search the excel sheet for the “name of .pdf file” in column A, then insert a keystroke to move to column B/C/D or E to paste the page numbers retrieved with the script from step 2 above.
-
The Applescript I wrote works only on an opened pdf file, so I’m wondering if there is a way to create a loop to open each file sequentially and apply the script to the opened file, then closing the opened file, so there are no opened window next time the loop repeats. I’ve tried working with the “For each item in a collection execute actions” action, but haven’t made any progress (unable to figure out how to open each of the files before running the other actions and then closing the file), and still not sure if this is the most efficient way to proceed with what I’m trying to accomplish.
As I mentioned in the beginning, I’m brand new to scripting (it took me the last week and a half to write the applescript to do the search in the pdf file), so please feel free to throw out suggestions, however basic. Here is the applescript I wrote–please do let me know if you can think of any ways I can improve it:
activate application "Preview"
tell application "System Events"
tell process "Preview"
set frontmost to true
click menu item "Find…" of menu 1 of menu item "Find" of menu 1 of menu bar item "Edit" of menu bar 1
delay 1
try
tell window 1
-- Perform the search
set keyWord to the clipboard
set value of text field 1 of group 3 of toolbar 1 to keyWord
delay 3
-- Get the search results from the sidebar
tell UI element 1 of rows 2 thru -1 of outline 1 of scroll area 1 of splitter group 1
set page_numbers to value of static text 1
end tell
end tell
return {page_numbers}
-- Error number -1719 means no keyword was found
-- if -1719 error displays, copy "NONE FOUND" into clipboard
on error error_message number error_number
if error_number is -1719 then
set the clipboard to "NONE FOUND"
else if error_number is -2753 then
display dialog "An error has occured:" & return & error_message
end if
end try
end tell
end tell
-- variable page_numbers not defined gives error -2753 from "page_numbers"
Thank you in advance,
Sincerely,
V.