Following on from Applescript Jekyll assistant (pt 1) we’re using a .plist to record the properties of the users’ blog posts that persist for the lifetime of the blog.

The script allows the user to choose where she keeps he Jekyll blog posts and move it around fer directory structure over time.

Today’s chunk reflects a better understanding of handling file references in either alias (class alias) or POSIX file (class text)

More chunks soon!

Open in Script Editor

-- these two properties are immutable 
property homeFolder : the POSIX path of (path to home folder)
property plistPath : the POSIX path of (homeFolder & ".makepost.plist")
(*
	We keep three persistent settings in 'makepost.plist':
	
	'chosenBlogPath' is the path to the folder where Jekyll expects blog posts to be.
	This is typically the '_posts' subdirectory in the Jekyll project files.
	
	'chosenCategories' is a list of the user's Categories from which the user can choose
	and which will appear in the frontmatter of the newly-to-be-created blog post's 'markdown'
	
	'chosenScriptFilePath' is the path to where the user has stored 'makepost.py'
	which is written to be used as a shell command for creating new Jekyll blog posts
	
*)
-- These 4 properties of our Script Object are mutable

-- not persistent across runs
property chosenTitle : "Untitled"

-- persistent across runs
property chosenBlogPath : the POSIX path of homeFolder
property chosenCategories : {"Blog", "Scripting"}
property chosenScriptFilePath : the POSIX path of (homeFolder & "bin/makepost.py")

(*	
	Initialisation
	-- -- -- -- --

	The plist, and thus the three persistent settings above may not yet exist
	in which case we have bootstrapped them with some sensible initial values.
	
	Notes:
	* AppleScript aliases have to refer to existing files
	
	* POSIX files and paths need not refer to existing files or directories
	
	* The AppleScript path of a POSIX path is "text"
*)


-- overwite the above persistent properties if the user already has a .plist
initialisePersistentSettings()


(*
	Next we ask the user
	1. What the title of her blog is going to be;
		and from this, we will derive the Jekyll-conformant file name for the post
	2. Which Folder she uses for posts
		in her Jekyll-conformant Folder structure
*)
-- overwite the above properties as the user chooses
manageTitleAndBlogpath()


-- DEBUG -- DEBUG -- DEBUG -- DEBUG -- DEBUG -- DEBUG -- DEBUG -- DEBUG --
log "chosenTitle: " & chosenTitle
log "chosenBlogPath: " & chosenBlogPath
log "chosenScriptFilePath: " & chosenScriptFilePath
repeat with n from 1 to the length of chosenCategories
	log "chosenCategories (" & n & "): " & item n of chosenCategories
end repeat
-- DEBUG -- DEBUG -- DEBUG -- DEBUG -- DEBUG -- DEBUG -- DEBUG -- DEBUG --


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
(*
	Handlers / subroutines
*)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

to manageTitleAndBlogpath()
	
	-- Reduce the length of the path for displaying as the legend in button 1
	set pathClue to crypticPath(chosenBlogPath)
	set chosenTitle to "Untitled"
	
	-- We'll cycle the path-choosing, not forgetting the title-choosing each cycle, until user is happy with her choice
	repeat
		set {OKButton, chosenTitle} to {button returned, text returned} of (display dialog "Choose the Title of your new post" default answer chosenTitle with title "Makepost" buttons {pathClue, "Other"} default button 1)
		
		if OKButton is pathClue then
			-- stay with the blogpath that we read from user's settings file
			exit repeat
		else
			chooseBlogPath()
			copy crypticPath(chosenBlogPath) to pathClue
		end if
		
	end repeat
	
end manageTitleAndBlogpath

to chooseBlogPath()
	
	-- User chooses her  blog posts Folder
	set chosenBlogPath to the POSIX path of (choose folder with prompt "Choose the path to your blog posts Folder" default location chosenBlogPath)
	
	-- record chosenBlogPath in makepost's persistent settings
	tell application "System Events"
		tell property list file plistPath
			set value of property list item "blogPath" to chosenBlogPath
		end tell
	end tell
end chooseBlogPath

on crypticPath(fullPath) -- (String) as String
	set splitPath to splitText(fullPath, "/")
	set lastBit to {}
	copy item ((count of splitPath) - 2) of splitPath to end of lastBit
	copy "/" to end of lastBit
	copy item ((count of splitPath) - 1) of splitPath to end of lastBit
	
	return ("Place in .../" & lastBit as text) & " ?"
end crypticPath

to 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


to initialisePersistentSettings()
	
	if not FileExists(plistPath) then
		
		-- create the plist with bootstrap settings now, and for recording the user's choices later
		tell application "System Events"
			
			-- Have a look at the 'Mac Automation Scripting Guide' for more on this 'tell' block
			
			-- Create an empty property list dictionary item
			set theParentDictionary to make new property list item with properties {kind:record}
			
			-- Create a new property list file using the empty dictionary list item as contents
			set thePropertyListFilePath to plistPath
			
			set thePropertyListFile to make new property list file with properties {contents:theParentDictionary, name:thePropertyListFilePath}
			
			tell property list items of thePropertyListFile
				
				-- use bootstrapped settings of these properties
				
				-- Add a list key for accessing the users' blog categories
				make new property list item at end with properties {kind:list, name:"catsList", value:chosenCategories}
				
				-- Add a string key for accessing the path to the user's blog posts
				make new property list item at end with properties {kind:string, name:"blogPath", value:chosenBlogPath}
				
				-- Add a string key for accessing the path to the python script 'makepost.py'
				make new property list item at end with properties {kind:string, name:"scriptFilePath", value:chosenScriptFilePath}
				
			end tell
		end tell
		
	else
		
		-- overwrite the bootstrapped values of these persistent properties
		tell application "System Events"
			
			tell property list file plistPath
				set chosenBlogPath to value of property list item "blogPath"
				set chosenCategories to value of property list item "catsList"
				set chosenScriptFilePath to value of property list item "scriptFilePath"
			end tell
			
		end tell
		
	end if
	
end initialisePersistentSettings

on FileExists(theFile) -- (String) as Boolean
	-- Convert the file to a string
	set theFile to theFile as string
	tell application "System Events"
		if exists file theFile then
			return true
		else
			return false
		end if
	end tell
end FileExists