Jump to content

File written by AppleScript not correctly recognized

Recommended Posts



my Jekyll workflow creates plain text files containing YAML frontmatter templates for the posts. So far the shell script actions work great, but I'm working on new commands that require AppleScript. I've basically finished one to create link posts, but...


The file written by the script looks like a regular file in the text editor, but the frontmatter is not correctly recognized by Jekyll. Strange thing: if I just copy the content, paste it into a new file in the editor and then save the file, it works flawlessly. Since the content is exactly the same I guess it must have to do something with the way the file is written by the script, but I have no idea how to solve that.


I tried using do shell script "echo " & quoted form of..." too, but it also didn't work.


Any hint would be greatly appreciated!


Here's the script:

-- on alfred_script(q)
set draftsPath to (path to home folder as text) & "path:to:Jekyll:_drafts:"
set postExtension to "md"
set q to "This is a test draft" -- Remove in Alfred
set postTitle to q
set postFilename to findReplace(space, "-", postTitle)
set postFilepath to draftsPath & postFilename & "." & postExtension
set postDate to todayISOformat()

if application "Safari" is running then
	tell application "Safari"
		set linkTitle to name of front document
		set linkUrl to URL of front document
		set linkQuote to (do JavaScript "(''+getSelection())" in document 1)
	end tell
	if (linkQuote is not "") then
		set linkQuote to return & ">" & space & linkQuote
	end if
	set frontMatter to "---" & return & "title: " & postTitle & return & "date: " & postDate & return & "layout: post" & return & "categories: []" & return & "tags: []" & return & "link: " & linkUrl & return & "---" & return & return & linkTitle & return & linkQuote as «class utf8»
	set af to open for access file postFilepath with write permission
	set eof af to 0
	write frontMatter to af as «class utf8»
	close access af
	tell application "Sublime Text"
		open postFilepath
	end tell

	display notification "Please open a page in Safari first." with title "Safari is not running!"

end if
-- end alfred_script

on findReplace(findText, replaceText, sourceText)
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to findText
	set sourceText to text items of sourceText
	set AppleScript's text item delimiters to replaceText
	set sourceText to "" & sourceText
	set AppleScript's text item delimiters to ASTID
	return sourceText
end findReplace

on todayISOformat()
	set theDate to current date
	set y to text -4 thru -1 of ("0000" & (year of theDate))
	set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
	set d to text -2 thru -1 of ("00" & (day of theDate))
	return y & "-" & m & "-" & d
end todayISOformat
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...