Skip to content

Instantly share code, notes, and snippets.

@freklov
Created April 2, 2017 09:45
Show Gist options
  • Save freklov/19e74697bfa9333297f597dac1d80b39 to your computer and use it in GitHub Desktop.
Save freklov/19e74697bfa9333297f597dac1d80b39 to your computer and use it in GitHub Desktop.
AppleScript for Things: Look for orphaned to dos of Next, Scheduled, Someday, Projects lists and assignes the tag "Lost+Found"
# things-lostfound freklov 04/02/2017
# Looks for orphaned to dos of Next, Scheduled, Someday, Projects lists and assignes the tag "Lost+Found"
#
# Requirements
# Requires a an tag named "Lost+Found"
property ThingsLostFoundTag : "Lost+Found"
# ensure that lost+found tag already exists
set tagLostFound to missing value
try
tell application "Things" to set tagLostFound to tag ThingsLostFoundTag
end try
if tagLostFound is missing value then
display notification "A tag with name " & quote & ThingsLostFoundTag & quote & ¬
" does not exist in Things. Please create it, first." with title "Tag does not exist" sound name "Blow"
return
end if
# define list of Things to process
-- set listThingsLists to {"Inbox", "Today", "Next", "Scheduled", "Someday", "Projects", "Logbook", "Trash"}
set listThingsLists to {"Next", "Scheduled", "Someday", "Projects"}
set countItems to 0
tell application "Things"
repeat with thingsList in listThingsLists
repeat with thingsToDo in to dos of list thingsList
try
set textAreaName to name of area of thingsToDo
on error
set textAreaName to ""
end try
try
set textProjectName to name of project of thingsToDo
on error
set textProjectName to ""
end try
if (textAreaName = "") and (textProjectName = "") then
set tag names of thingsToDo to my updatedToDoTags({existingTags:tag names of thingsToDo, newTags:ThingsLostFoundTag})
set countItems to countItems + 1
end if
end repeat
end repeat
end tell
if countItems > 0 then
display notification "Added tag " & quote & ThingsLostFoundTag & quote & "to " & (countItems as text) & " to dos." sound name "Purr"
else
display notification "No orphaned items found. " sound name "Purr"
end if
# take existing tags from an to do and add additional tags
# input
# existingTags: comma separated string of tags
# newTags: comma separated string of new tags
#
# Return
# comma separate string of joined tags
#
on updatedToDoTags(inputValues)
tell application "Things"
set listTags to my splitText(existingTags of inputValues, ",")
set newTags to my splitText(newTags of inputValues, ",")
repeat with newTag in newTags
if listTags does not contain newTag then copy newTag as text to end of listTags
end repeat
set returnValue to my convertListToString(listTags, ",")
end tell
return returnValue
end updatedToDoTags
# source Mac Scripting Guide
# https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/index.html#//apple_ref/doc/uid/TP40016239-CH56-SW1
on splitText(theText, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
return theTextItems
end splitText
on convertListToString(theList, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to ""
return theString
end convertListToString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment