This script will look for saved searches in two places:
(Both the project name and file name/location can be modified by changing the first two lines of the script.)
The syntax for the saved searches is "?? Name ?? query" (without quotation marks) or "?? query" (again without quotation marks). The saved searches can be entered as tasks or notes, but each search must be given its own line in the document. Any line in the Search Definitions project or Search Definitions.taskpaper file that does not contain ?? is ignored.
Upon invoking the script, the user is presented with a list of queries to choose from; if there is a name specified, that is used to label the query, but if no name is specified, then the query itself is used.
The script is available HERE.
Store the script in TaskPaper's Scripts Folder and assign it a keyboard shortcut with the Keyboard and Mouse Preferences Pane. Here's are the steps in detail. Open TaskPaper's Scripts Folder from "Open Scripts Folder" item in the Scripts Menu (It's the one to the left of the Help menu.) Drag the script into the folder and close. "Choose Saved Search" will appear as an item in the Scripts Menu.
To assign a keyboard shortcut:
Open the Keyboard and Mouse Preference Pane in System Preferences.
Click on the right pane "Keyboard Shortcuts" and scroll down to the end.
Highlight the line that says "Application Keyboard Shortcuts" and click the + button at the bottom.
A new window will open up. Open the Application pop-up list and select TaskPaper.
In the "Menu Title" box enter "Choose Saved Search" (no quotes).
Choose a keyboard shortcut and click "Add". (I use option-command- j)
Switch to TaskPaper and test your shortcut
When you are satisfied that it's working, close System Preferences, and you are done.
Here is the text of the script:
set search_file_location to (POSIX path of (path to home folder) as string) & "Documents/TaskPaper/Search Definitions.taskpaper"
set search_project_name to "Search Definitions"
set exclude_search_project_in_results to false
set search_delimiters to "??"
global search_string
set search_file_exists to true
tell application "System Events"
set search_file_exists to (exists file search_file_location)
end tell
if search_file_exists then
set search_file to POSIX file search_file_location
open for access search_file
set raw_searches to read search_file using delimiter {ASCII character 10}
close access search_file
else
set raw_searches to {}
end if
tell application "TaskPaper"
tell front document
set search_string to search field string
try
set p to project named search_project_name
set raw_searches to raw_searches & text line of every entry of p
end try
end tell
end tell
if raw_searches is equal to {} then
display dialog "No searches were found. Define queries either 1) inside a project named \"" & search_project_name & "\" in the current document; or 2) inside the document \"" & search_file_location & "\". The file location and project name can be changed by modifying the first and second lines of this script, respectively."
else
try
set AppleScript's text item delimiters to {search_delimiters} -- declare new delimiters to parse searches
set search_names to {}
set searches to {}
set delimited_search to {}
repeat with raw_search in raw_searches -- split the searches into names and queries
set delimited_search to every text item of raw_search
set length_of_delimited_search to length of delimited_search
if length_of_delimited_search is greater than 1 then
if (length_of_delimited_search is equal to 2) then
copy (my trim(true, (item 2 of delimited_search))) to the end of search_names
copy (my trim(true, (item 2 of delimited_search))) to the end of searches
else
set blank_name to ((my trim(true, item 2 of delimited_search)) is equal to "") -- ignore blank names
set rebuilt_query to (item 3 of delimited_search)
if (length_of_delimited_search is greater than 3) then
-- the query had some ??'s in it; we need to rebuild it.
repeat with counter from 4 to length_of_delimited_search
set rebuilt_query to rebuilt_query & search_delimiters & (item counter of delimited_search)
end repeat
end if
set rebuilt_query to (my trim(true, rebuilt_query))
if blank_name then -- use the query for the name
copy rebuilt_query to the end of search_names
else -- use the name for the name
copy (my trim(true, (item 2 of delimited_search))) to the end of search_names
end if
copy rebuilt_query to the end of searches
end if
end if
end repeat
set choice to (choose from list search_names with prompt {"Available Seaches"} default items "") -- get the desired search name
if (choice) is false then
return
else -- find the query corresponding to the search name
set search_index to 0
repeat with i from 1 to (count of items in search_names)
if (item i of search_names as string) is equal to (choice as string) then
set search_index to i
exit repeat
end if
end repeat
if search_index is equal to 0 then
return
else
set search_string to item search_index of searches
end if
end if
set AppleScript's text item delimiters to {""} -- restore them
on error
set AppleScript's text item delimiters to {""} -- restore them in case something went wrong
end try
end if
if exclude_search_project_in_results then
set search_string to "(not project \"" & search_project_name & "\") and " & search_string
end if
tell application "TaskPaper"
tell front document
set search field string to search_string
end tell
end tell
on trim(trim_chars, some_text)
-- Lazy default (AppleScript doesn't support default values)
if trim_chars is true then set trim_chars to ¬
{" ", tab, ASCII character 10, return, ASCII character 0}
repeat until first character of some_text is not in trim_chars
try
set some_text to text 2 thru -1 of some_text
on error
-- the text contains nothing but trim_chars
return ""
end try
end repeat
repeat until last character of some_text is not in trim_chars
try
set some_text to text 1 thru -2 of some_text
on error
-- the text contains nothing but trim_chars
return ""
end try
end repeat
return some_text
end trim