Task Manager Script

I’m pretty sure many of you have made this, but here is a app to help you manage your day:

property thefolder : ((path to preferences folder from user domain as Unicode text) & "Tasks:" as Unicode text)
property s : missing value
repeat
	tell application "Finder"
		if not (exists thefolder) then make new folder at folder ((path to preferences folder from user domain as Unicode text)) with properties {name:"Tasks"}
	end tell
	set TimeStamp to do shell script "/bin/date -n +%m-%d-%Y' '%H.%M.%S" as string
	set storelocation to (thefolder & TimeStamp) as string
	tell application "Finder" to set thecount to count every item of folder thefolder
	if thecount is 0 then
		set thelist to {"Create Task", "Delete Task", "Quit"}
	else
		set s to "s"
		if thecount is 1 then set s to ""
		set thelist to {"View Tasks - " & thecount & " task" & s, "Create Task", "Delete Task", "Quit"}
	end if
	set action to (choose from list thelist with prompt "What do you want to do?" with title "Tasks") as string
	if s is missing value then set s to ""
	if action is ("View Tasks - " & thecount & " task" & s) then
		view()
	else if action is "Create Task" then
		set Textinput to text returned of (display dialog "Huh?" default answer "Here." buttons {"OK"} default button 1) as string
		set writeto to tofile(Textinput, storelocation)
		if writeto is false then
			display dialog "An error has occured."
		end if
	else if action is "Delete Task" then
		deletetask()
	else
		exit repeat
	end if
end repeat

on tofile(value, place)
	try
		set towrite to open for access file place with write permission
		write value to towrite
		close access towrite
		return true
	on error
		try
			close access file towrite
		end try
		return false
	end try
end tofile

on view()
	set thefolder to thefolder
	tell application "Finder"
		if (exists thefolder) then
			set folder_list to name of every item of folder thefolder
		end if
	end tell
	set thelistread to {}
	repeat with i in folder_list
		set readed to read file (thefolder & i)
		set thelistread to thelistread & readed
	end repeat
	if thelistread is not {} then
		repeat
			set yesorno to choose from list thelistread with prompt "Here is your tasks:"
			if yesorno is false then exit repeat
		end repeat
	else
		display dialog "You have no tasks. You are now board." buttons {"OK"} default button 1
	end if
end view

on deletetask()
	tell application "Finder"
		if (exists thefolder) then
			set folder_list to name of every item of folder thefolder
		end if
	end tell
	set thelistread to {}
	repeat with i in folder_list
		set readed to read file (thefolder & i)
		set thelistread to thelistread & readed
	end repeat
	if thelistread is not {} then
		set theitem to (choose from list thelistread with prompt "Which item do you want to delete?") as string
		set numb to 0
		repeat with i in thelistread
			set i to i as string
			set numb to numb + 1
			if i is theitem then
				exit repeat
			end if
		end repeat
		set deletefile to thefolder & (item numb of folder_list)
		tell application "Finder" to delete file deletefile
		return
	else
		display dialog "You have nothing to delete!" buttons {"OK"} default button 1
	end if
end deletetask

Very nice.

Hope you do not mind me saying so…
Some pointers in what its supposed to do might be a good idea.

Also some management on same name tasks may help.

Example. if some tasks ( with extra info) are called:

Here.
Here.
Here1

and you select either of the Here. tasks. you will be forced to view both.
I think it may be best to either let the user know they have a task with the same name (as the list may be very long, and all tasks not in view) or automatically add something on front or the end of the tasks.

Another behaviour I noticed is, you get dumped straight back into the view task selection if the selected task has no extra info.

How about adding a prefix to the task name to show it has extra info.
You could also use a separator string in the txt files rather than read/write to two files for each task with extra info…
Example contents:

You then delimitate using the separator**

text item 1 being the task
text item 2 being the task extra info

Can someone help me? I need help. Look at the deletetask() function I have in this script. I need help to improve it. It sometimes deletes the wrong thing:

property thefolder : ((path to preferences folder from user domain as Unicode text) & "Tasks:" as Unicode text)
property s : missing value
repeat
	activate
	tell application "Finder"
		if not (exists thefolder) then make new folder at folder ((path to preferences folder from user domain as Unicode text)) with properties {name:"Tasks"}
	end tell
	set TimeStamp to do shell script "/bin/date -n +%m-%d-%Y' '%H.%M.%S" as string
	set storelocation to (thefolder & TimeStamp) as string
	tell application "Finder" to set thecount to count (items of folder thefolder whose name does not end with "_info")
	if thecount is 0 then
		set thelist to {"Create Task", "Quit"}
	else
		set s to "s"
		if thecount is 1 then set s to ""
		set thelist to {"View Tasks - " & thecount & " task" & s, "Edit Task", "Create Task", "Delete Task", "Quit"}
	end if
	set action to (choose from list thelist with prompt "What do you want to do?" with title "Tasks") as string
	if s is missing value then set s to ""
	if action is ("View Tasks - " & thecount & " task" & s) then
		view()
	else if action is "Edit Task" then
		set answer to edit()
		if edit is false then display dialog "An error has occured!"
	else if action is "Create Task" then
		set dialog to (display dialog "Huh?" default answer "Here." buttons {"Create", "Create with more infomation"} default button 1)
		set Textinput to text returned of dialog as string
		set writeto to tofile(Textinput, storelocation)
		if writeto is false then
			display dialog "An error has occured."
		else
			if button returned of dialog is not "Create" then
				set moreinfo to text returned of (display dialog "More infomation?" default answer return & return & return buttons {"OK"} default button 1 with title "Info?")
				set writeto to tofile(moreinfo, (storelocation & "_info"))
			end if
		end if
	else if action is "Delete Task" then
		deletetask()
	else
		exit repeat
	end if
	set s to missing value
end repeat

on tofile(value, place)
	tell application "Finder"
		if (exists file place) then
			delete file place
		end if
	end tell
	try
		set towrite to open for access file place with write permission
		write value to towrite
		close access towrite
		return true
	on error
		try
			close access file towrite
		end try
		return false
	end try
end tofile

on view()
	set thefolder to thefolder
	tell application "Finder"
		if (exists thefolder) then
			set folder_list to name of every item of folder thefolder
		end if
	end tell
	set thelistread to {}
	repeat with i in folder_list
		if i does not end with "_info" then
			set readed to read file (thefolder & i)
			set thelistread to thelistread & readed
		end if
	end repeat
	if thelistread is not {} then
		repeat
			set yesorno to (choose from list thelistread with prompt "Here is your tasks:")
			if yesorno is false then exit repeat
			set numb to 0
			repeat with i in folder_list
				set numb to numb + 1
				tell application "Finder"
					if (exists file (thefolder & i & "_info")) then
						set toread to read file (thefolder & i)
						set var1 to (read file (thefolder & i))
						set var2 to (read file (thefolder & i & "_info"))
						if var1 is (yesorno as string) then
							set dialog to "Info for \"" & toread & "\"" & return & return & return & var2
							set display to {true, numb}
						else
							set display to {false, 0}
						end if
					else
						set display to {false, 0}
					end if
				end tell
				if (item 1 of display) is true and (item 2 of display) is numb then
					display dialog dialog
				end if
				set display to {false, 0}
			end repeat
		end repeat
	else
		display dialog "You have no tasks. You are now board." buttons {"OK"} default button 1
	end if
end view

on deletetask()
	tell application "Finder"
		if (exists thefolder) then
			set folder_list to name of every item of folder thefolder
		end if
	end tell
	set thelistread to {}
	repeat with i in folder_list
		if i does not end with "_info" then
			set readed to read file (thefolder & i)
			set thelistread to thelistread & readed
		end if
	end repeat
	repeat with i in folder_list
		set readed to read file (thefolder & i)
		set thelistread2 to thelistread & readed
	end repeat
	if thelistread is not {} then
		set theitem to (choose from list thelistread with prompt "Which item do you want to delete?") as string
		set numb to 0
		repeat with i in thelistread2
			set i to i as string
			set numb to numb + 1
			if i is theitem then
				exit repeat
			end if
		end repeat
		if theitem is not false then
			set deletefile to thefolder & (item numb of folder_list)
			tell application "Finder" to if (exists thefolder & (item numb of folder_list) & "_info") then
				set deletefile2 to thefolder & (item numb of folder_list) & "_info"
				tell application "Finder"
					delete file deletefile
					delete file deletefile2
				end tell
			else if not (exists thefolder & (item numb of folder_list) & "_info") then
				tell application "Finder" to delete file deletefile
			end if
		end if
		return
	else
		display dialog "You have nothing to delete!" buttons {"OK"} default button 1
	end if
end deletetask

on edit()
	tell application "Finder"
		if (exists thefolder) then
			set folder_list to name of every item of folder thefolder
		end if
	end tell
	set thelistread to {}
	repeat with i in folder_list
		if i does not end with "_info" then
			set readed to read file (thefolder & i)
			set thelistread to thelistread & readed
		end if
	end repeat
	set edititem to (choose from list thelistread with prompt "Which item do you want to edit?") as string
	set numb to 0
	repeat with i in folder_list
		if i does not end with "_info" then
			set readed to (read file (thefolder & i)) as string
			if readed is edititem then
				set editinfo to (display dialog "Please put in the task to rename:" default answer readed buttons {"Edit", "Edit with more information"} default button 1 with title "Edit")
				set rename to text returned of editinfo
				set buttonre to button returned of editinfo as string
				if buttonre is "Edit with more information" then
					set infoinfo to text returned of (display dialog "Please put in the infomation:" default answer "" buttons {"Finish"} default button 1 with title "Edit")
					tell application "Finder" to if not (exists file (thefolder & i & "_info")) then if infoinfo is "" then set infoinfo to missing value
				else
					set infoinfo to missing value
				end if
				set answer to tofile(rename, (thefolder & i))
				if infoinfo is not missing value then
					set answer2 to tofile(infoinfo, (thefolder & i & "_info"))
				else
					set answer2 to true
				end if
				if answer is true and answer2 is true then
					return true
				else
					return false
				end if
			end if
		end if
	end repeat
end edit

Help!?!? :frowning:

HELP! OHZ NOZ! I am trying to fix it, but nothing looks wrong. Am I stupid and missing something?

No help, but a note:

NEVER use a property with a relative path definition (path to).
In the moment you’re going to use the script on a different computer (even in an user account on the same computer) the script will fail, because the path is actually “hard-coded”

Hi, Dylan.

I’ve had a look at the handler code, but not in the context of the rest of the script (which would be an evening’s study in itself without any comments to show what’s supposed to be happening). Hope there’s something useful in my comments below.

on deletetask()
	tell application "Finder"
		if (exists thefolder) then
			set folder_list to name of every item of folder thefolder
		end if
	end tell
	
	-- 1. 'thefolder' is a text path. Although using it by itself instead of as part of a proper specifier sometimes works (as in the 'exists' command above), sometimes it doesn't (which is why you've had to use the 'folder' keyword in the next line). It's better to use the correct syntax wherever possible.
	-- 2. This handler contains no provision for what to do when 'thefolder' DOESN'T exist and will error when it tries to loop through uncreated 'folder_list' below.
	
	set thelistread to {}
	repeat with i in folder_list
		if i does not end with "_info" then
			set readed to read file (thefolder & i)
			set thelistread to thelistread & readed
		end if
	end repeat
	repeat with i in folder_list
		set readed to read file (thefolder & i)
		set thelistread2 to thelistread & readed
	end repeat
	
	-- 'thelistread2' gets set to something different each time round the above repeat, always ending up as a copy of 'thelistread' (a list containing texts read from all the 'items' in the folder (hopefully text files) whose names don't end with "_info") with an additional text read from the last 'item' whose name is in 'folder_list', whether or not this name ends with "_info".
	
	if thelistread is not {} then
		set theitem to (choose from list thelistread with prompt "Which item do you want to delete?") as string
		set numb to 0
		repeat with i in thelistread2
			set i to i as string
			set numb to numb + 1
			if i is theitem then
				exit repeat
			end if
		end repeat
		
		-- It's not clear why you've bothered with 'thelistread2', since you could easily find the 'numb' index from 'thelistread' itself.
		-- repeat with numb from 1 to (count thelistread)
		-- 	if (item numb of thelistread is theitem) then exit repeat
		-- end repeat
		
		if theitem is not false then
			
			-- This 'if' line would be better placed immediately after the 'choose from list' line further up. However, since the 'choose from list' result is coerced to string, 'theitem' is never the boolean false but the string "false", so the code below is always executed, with the possible errors that implies.
			
			set deletefile to thefolder & (item numb of folder_list)
			
			-- 'numb' has been set against a list containing texts read from items whose names don't end with "_info". But 'folder_list' contains the names of ALL the items in the folder. 'item numb of folder_list' might not be the name of the numbth item in the folder whose name doesn't end with "_info"'.
			
			tell application "Finder" to if (exists thefolder & (item numb of folder_list) & "_info") then
				set deletefile2 to thefolder & (item numb of folder_list) & "_info"
				tell application "Finder"
					
					-- This 'tell' block is unnecessary as we're already covered by the 'tell application "Finder"' two lines earlier.
					
					delete file deletefile
					delete file deletefile2
				end tell
			else if not (exists thefolder & (item numb of folder_list) & "_info") then
				
				-- This 'else' section is only entered if the item doesn't exist, so there's no need to test for its non-existence again after the 'else' keyword. It would take less code to delete file deletefile anyway and then to delete the "_info" version if it exists.
				
				tell application "Finder" to delete file deletefile
				
				-- Unnecessary 'tell'.
				
			end if
		end if
		return
	else
		display dialog "You have nothing to delete!" buttons {"OK"} default button 1
	end if
end deletetask