#!/usr/bin/env ruby # -*- coding: utf-8 -*- require File.join(File.dirname(__FILE__), *%w".. lib Things2THL") require "optparse" require "ostruct" require "time" options=Things2THL.default_options opts = OptionParser.new do |opts| opts.banner = "Usage: things2thl [options]" def opts.show_usage puts self exit end def yesno(v) "(default: #{v ? 'yes' : 'no'})" end opts.separator '' opts.separator("Modes of operation (required):") opts.on("-L", "--projects-as-lists", "Convert projects in Things to lists in THL" ) { options.structure = :projects_as_lists } opts.on("-T", "--projects-as-tasks", "Convert projects in Things to tasks in THL" ) { options.structure = :projects_as_tasks } opts.on("-B", "--projects-areas-as-tasks", "Convert both projects and areas in Things", " to lists in THL. This implies that", " projects are not nested inside areas.") { options.structure = :projects_areas_as_lists } opts.on("-I", "--inbox", "Transfer only Inbox tasks." ) do options.inboxonly = true options.structure = :projects_as_lists end opts.separator '' opts.separator("Options:") opts.on("--[no-]areas", "Transfer areas from Things #{yesno(options.areas)}") { |v| options.areas = v } opts.on("--areas-as-tags", "Transfer areas as tags in THL.") { options.areas_as=:tags } opts.on("--areas-as-contexts", "Transfer areas as contexts in THL.") { options.areas_as=:contexts } opts.on('--[no-]time-tags', 'Consider tags of the form Xmin/Xsec/Xhr as', ' time estimates, set them in THL', " accordingly #{yesno(options.timetags)}.") do |value| options.timetags = value end opts.on('--[no-]context-tags [REGEX]', 'Regular expression to identify tags that', ' should be interpreted as contexts.', " (default: #{options.contexttagsregex}).", " Use with no- to disable this feature.") do |regex| options.contexttagsregex = case regex; when false:nil; when "":options.contexttagsregex; else regex; end end opts.on("--sync", "Only transfer new items #{yesno(options.sync)}") { options.sync = true } opts.on("--top-level-folder FOLDER", "Do the import inside the named folders,"," instead of the top level", " (Inbox, etc. will also be created there"," instead of their corresponding places)") do |toplevel| options.toplevel = toplevel end opts.on("--projects-top-level FOLDER", "The named folder will be created to", " contain all projects when", " --projects-as-lists is used (otherwise", " they will be in the top folders group).", " If --projects-as-tasks is used, a", " 'Projects' list is always created, but", " this option can be used to specify", " its name.") do |projfolder| options.projectsfolder = projfolder end opts.on("--areas-top-level FOLDER", "The named folder will be created to", " contain all areas.") do |areafolder| options.areasfolder = areafolder end opts.on("-c", "--completed", "Transfer also completed/canceled tasks", " and projects #{yesno(options.completed)}") { options.completed = true } opts.on("--[no-]archive-completed", "If transferring completed/canceled tasks,", " also mark them as archived #{yesno(options.archivecompleted)}") {|v| options.archivecompleted = v } opts.on("-q", "--quiet", "Do not print items as they are processed.", "Twice: do not print stats at the end.") { options.quiet+=1 } # opts.on("-n", "--dry-run", "Do not create anything in THL, just print the items that would be created") { options.dryrun = true } # opts.on("-n", "--notes", "Shows only tasks with notes") { options[:tasks] = { :onlynotes => true } } # opts.on("-a", "--all", 'Shows all tasks in the focus') { |f| options[:tasks] = { :children => false } } opts.on("-h", "--help", "Shows this help message") { opts.show_usage } opts.on("-v", "--version", "Shows version") do puts Things2THL::Version::STRING exit end opts.separator("") opts.separator("Options you should seldom need:") opts.on("--things THINGSAPP", "Location of the Things application", " (default: /Applications/Things.app)") do |things| options.thingsapp = things end opts.on("--thl THLAPP", "Location of the The Hit List application", " (default: /Applications/The Hit List.app)") do |thl| options.thlapp = thl end end ###################################################################### # Main program ###################################################################### opts.parse! unless options.structure || options.inboxonly puts "Error: you didn't specify the mode of operation to use." puts '' opts.show_usage end converter = Things2THL.new(options, options.thingsapp, options.thlapp) things = converter.things thl = converter.thl # Start timing start=Time.new unless options.inboxonly # First, traverse all areas if options.areas && !options.areas_as puts "Processing Areas of Responsibility" unless options.quiet.nonzero? things.areas.get.each do |area| converter.process(Things2THL::ThingsNode.new(area)) end end # Next, traverse all projects, putting each one inside its corresponding area puts "Processing Projects" unless options.quiet.nonzero? if options.completed puts " (fetching all projects - this may take a while)" unless options.quiet.nonzero? projlist=things.projects.get else puts " (fetching only open projects - this may take a while)" unless options.quiet.nonzero? projlist=things.projects[its.status.eq(:open)].get end projlist.each do |project| converter.process(Things2THL::ThingsNode.new(project)) end end # Now do the tasks # This is more complicated because: # - to_dos returns not only tasks, also projects (not areas) # - to-dos returns tasks from all the views: Inbox, Today, Scheduled, Someday, and Next, so we have # to separate them and create the appropriate containers as needed puts "Processing tasks" unless options.quiet.nonzero? if options.inboxonly queryobj=things.lists['Inbox'] else queryobj=things end if options.completed puts " (fetching all tasks - this may take a while)" unless options.quiet.nonzero? tasklist=queryobj.to_dos.get else puts " (fetching only open tasks - this may take a while)" unless options.quiet.nonzero? tasklist=queryobj.to_dos[its.status.eq(:open)].get end tasklist.each do |t| task=Things2THL::ThingsNode.new(t) next if task.type != :selected_to_do converter.process(task) end # End timing diff=Time.new-start # Print stats at the end unless options.quiet >= 2 puts "Conversion done in #{diff} seconds." converter.created.each_pair { |key,val| puts " #{val} #{key}#{val!=1 ? 's' : ''} created" } end