Help Text

Hi,
here an experiment.
Because my brother is new to macs and computers in general, I wrote him a little app to assist his learning process for the applications he uses.
The little helper I wrote accepts visual content, like images (via drag and drop only) and text-input which you have to write yourself. :wink:
In practice, you write your own help files; the images are snapshots and like the text, a reference material to remember the functions, tips and tricks of a particular application.
The build-in ‘Help viewer’ is a bit “impersonal”, for my taste. Texts written in a certain syntax aren’t always clear because they use some formulas that aren’t in use by common people, especially computer-related terminology. That does not mean that those people has not the right to understand. Everybody has his/her own knowledge, so a personal help viewer could prove to be useful.

Tip: add a funny icon to this app, because reading AND understanding should be easily and at a daily practice order.

on run
	set {getProc, tutor_fl} to my retrieve_apps()
	--choose mode
	activate me
	display dialog "HELP TEXT" & return & return & "Read or write help files ?" buttons {"Write", "Read", "Cancel"} default button 1 with icon 1
	set the_b to the button returned of the result
	if the_b is "Write" then
		my write_file(getProc, tutor_fl)
	else if the_b is "Read" then
		my read_file(getProc, tutor_fl)
	end if
end run

on open this_files
	#accept image types only
	set img_list to {"jp2", "jpg", "jpeg", "tif", "tiff", "png", "gif", "pict", "QuickTime Image", "psd", "svg", "bmp", "tga"}
	set img_lib to {}
	
	repeat with a in this_files
		tell application "System Events"
			set {name_file, file_ex} to {name, name extension} of alias (a as text)
		end tell
		if file_ex is in img_list then copy a to end of img_lib
	end repeat
	
	if img_lib is not {} then
		set {all_apps, tutor_fl} to my retrieve_apps()
		
		activate
		set get_app to choose from list all_apps with prompt "Associate Pictures to."
		if get_app is false then return
		
		try
			do shell script "mkdir -p '" & POSIX path of (tutor_fl & get_app as text) & "'"
		end try
		set help_files to (tutor_fl & get_app as text)
		tell application "Finder"
			move img_lib to alias help_files
		end tell
	end if
end open

on retrieve_apps()
	set tutor_fl to ((path to applications folder as text) & "Tutor:" as text)
	--create default dir
	try
		set cmd to "mkdir " & quoted form of POSIX path of tutor_fl
		do shell script cmd
	end try
	--get running processes
	set Applescript_apps to {}
	tell application "System Events"
		set all_apps to name of processes whose background only is false and name is not "Finder" and name is not "droplet" and name is not "applet" and name is not "Help Text"
		
		set Applescript_apps to title of processes whose background only is false and name is "droplet" or name is "applet" and title is not "Help Text"
		if Applescript_apps is not {} then set all_apps to all_apps & Applescript_apps
	end tell
	set nw_ls to {}
	repeat with i in all_apps
		if i is not in missing value and i is not in {"droplet", "applet", "Help Text"} then copy i to end of nw_ls
	end repeat
	set all_apps to nw_ls
	--sort alphabetically
	set getProc to tagSort(all_apps)
	#return results
	return {getProc, tutor_fl}
end retrieve_apps

on read_file(getProc, tutor_fl)
	set del to "_________________________"
	activate
	tell application "System Events"
		set help_files to name of items of alias (tutor_fl as text) whose name ends with ".txt"
		set help_picts to name of folders of alias (tutor_fl as text)
	end tell
	if help_files is {} and help_picts is {} then
		activate
		display dialog "NO help files found !" & return & return & "Write first some Text OR add pictures via drag and drop to create some help files first." buttons "Abort." default button 1
		return
	else
		#check unique names
		set dd to 0
		repeat with a in help_picts
			if (a & ".txt") is not in help_files then
				set dd to dd + 1
				if dd = 1 then
					set help_files to help_files & del & a
				else
					copy a to end of help_files
				end if
			end if
		end repeat
		
		set {add_picts, add_txt} to {false, false}
		set ch to choose from list help_files with prompt "Choose a Help file." with title "READ HELP FILES" default items (item 1 of help_files)
		if ch is false then return
		set ch to ch as text
		if ch ends with ".txt" then
			set pict_fl to text 1 thru -5 of ch
			set doc_fl to ch
		else
			set pict_fl to ch
			set doc_fl to ch & ".txt"
		end if
		
		set picture_ref to (tutor_fl & pict_fl & ":" as text)
		tell application "System Events"
			if exists alias picture_ref then set add_picts to true
			if exists alias (tutor_fl & doc_fl as text) then set add_txt to true
		end tell
		
		#check for visual help
		if add_picts is true then
			set lf_pict to list folder picture_ref without invisibles
			#collect picts
			set pict_files to {}
			repeat with a in lf_pict
				set a to a as text
				copy "'" & POSIX path of (picture_ref & a as text) & "' " to end of pict_files
			end repeat
			set pict_files to pict_files as text
			try
				do shell script "open -a 'Preview' " & pict_files
			end try
			delay 0.5
		end if
		#priority has text
		if add_txt is true then
			do shell script "open " & quoted form of POSIX path of (tutor_fl & ch as text)
		end if
	end if
end read_file

on write_file(getProc, tutor_fl)
	set del to "_________________________"
	activate
	set ch to choose from list getProc with prompt "Choose an Application." with title "WRITE HELP FILES" default items (item 1 of getProc)
	
	if ch is false then return
	set ch to ch as text
	set inputFile to (tutor_fl & ch & ".txt" as text)
	activate
	set write_txt to the text returned of (display dialog "TUTOR" & return & return & "Write Help text for '" & ch & "'" & return default answer "Title" & return & "Description" & return)
	if write_txt is not "" then
		set shdt to short date string of (current date)
		set add_txt to shdt & return & return & write_txt & return & del & return
		set rewrite to false
		my write_note(inputFile, add_txt, rewrite)
	end if
end write_file

--SUPPL ROUTINES
-----------------------------------------------------------------------------------
on write_note(inputFile, to_write, rewrite)
	--universal
	try
		open for access file the inputFile with write permission
		if rewrite is true then
			write to_write to file the inputFile --starting at eof
		else
			write to_write to file the inputFile starting at eof
		end if
		close access file the inputFile
	end try
end write_note

to tagSort(L) -- a general purpose simple sort; not enough tags to get fancy.
	considering case
		set sl to {}
		set IL to {}
		repeat (count L) times
			set the low_item to ""
			repeat with i from 1 to (count L)
				if i is not in the IL then
					set this_item to item i of L as text
					if the low_item is "" then
						set the low_item to this_item
						set the low_item_index to i
					else if this_item comes before the low_item then
						set the low_item to this_item
						set the low_item_index to i
					end if
				end if
			end repeat
			set the end of sl to the low_item
			set the end of the IL to the low_item_index
		end repeat
	end considering
	return sl
end tagSort