Importing Categories, Projects and Time Entries from Tyme 2

Simply copy the following script(s) into a new "Script Editor" document and press run.
Feel free to customize them for your use cases.

Note: We do not take responsibility for any data loss incurred by running these scripts.
Make sure to back up your data (e.g. by copying the directories mentioned here to a different location) before running these scripts.

-- Copyright (c) 2017 timingapp.com / Daniel Alm. All rights reserved.
-- This script is licensed only to extend the functionality of Timing. Redistribution and any other uses are not allowed without prior permission from us.

set importStartDate to ((current date) - 10 * 365 * 24 * 3600)
set importEndDate to current date

set alertResult to display alert "Please back up your data!" message "Before proceeding, please make sure to back up your Timing database.\n\nSee https://timingapp.com/help/faq#data on which folders you need to back up." buttons {"Cancel", "I Have A Backup"}
if button returned of alertResult is "Cancel" then
	error number -128
end if

on getOrCreateProject(projectName, parentProject)
	if projectName = "" then
		return missing value
	end if
	
	tell application "TimingHelper"
		if parentProject is missing value then
			set results to (projects whose name is projectName)
			if results = {} then
				return (create project name projectName)
			else
				return (first item of results)
			end if
		else
			set results to (projects of parentProject whose name is projectName)
			if results = {} then
				return (create project name projectName parent project parentProject)
			else
				return (first item of results)
			end if
		end if
	end tell
end getOrCreateProject

tell application "Tyme2"
	set cats to categories
	set projs to projects
	set allTasks to every task of every project
	GetTaskRecordIDs startDate importStartDate endDate importEndDate
	set taskRecordIDs to fetchedTaskRecordIDs
end tell

repeat with cat in cats
	getOrCreateProject(get name of cat, missing value)
end repeat

repeat with proj in projs
	tell application "Tyme2"
		set catName to ""
		try
			set catName to (name of front category whose id is categoryID of proj)
		end try
	end tell
	
	getOrCreateProject(get name of proj, getOrCreateProject(catName, missing value))
end repeat

repeat with taskRecordID in taskRecordIDs
	tell application "Tyme2"
		GetRecordWithID taskRecordID
		get properties of lastFetchedTaskRecord
		set startDate to timeStart of lastFetchedTaskRecord
		set endDate to timeEnd of lastFetchedTaskRecord
		get timedDuration of lastFetchedTaskRecord
		set catID to relatedCategoryID of lastFetchedTaskRecord
		set projectID to relatedProjectID of lastFetchedTaskRecord
		set taskID to relatedTaskID of lastFetchedTaskRecord
		-- Note: Importing subtasks is currently not supported (because I don't know how to get their properties from Tyme).
		--set subtaskID to relatedSubTaskID of lastFetchedTaskRecord
		set tymeProject to (front project whose id is projectID)
		set projectName to name of tymeProject
		set allTaskNames to (name of (every task of tymeProject) whose id is taskID)
		set categoryName to ""
		try
			set categoryName to name of (front category whose id is catID)
		end try
	end tell
	
	set taskName to item 1 of allTaskNames
	set timingProject to getOrCreateProject(projectName, getOrCreateProject(categoryName, missing value))
	
	if startDate is greater than endDate then
		-- swap start and end date if neeeded
		set tmp to startDate
		set startDate to endDate
		set endDate to tmp
	end if
	
	if endDate is greater than startDate then
		-- ignore zero-duration tasks
		tell application "TimingHelper"
			add task from startDate to endDate with description taskName project timingProject
		end tell
	end if
end repeat