Jump to content

AppleScript works fine in AS-Editor but not in workflow


Recommended Posts

Hi,

 

I'm working on a link post command for my Jekyll workflow. It's an AppleScript that reads the a URL, its title and the selected text from Safari and then creates a Jekyll draft with this information filled in. It works fine from the AppleScript editor, but when I run it from Alfred, I get the following error:

[ERROR: alfred.workflow.action.applescript] {
    NSAppleScriptErrorBriefMessage = "Expected \U201cend\U201d but found \U201con\U201d.";
    NSAppleScriptErrorMessage = "Expected \U201cend\U201d but found \U201con\U201d.";
    NSAppleScriptErrorNumber = "-2741";
    NSAppleScriptErrorRange = "NSRange: {1002, 2}";

 Here's the script as I run it in the AppleScript editor:

set draftsPath to (path to home folder as text) & "jekyll:site:_drafts:"

set q to "This is a test draft"
set linkpostTitle to q
set linkpostFilename to findReplace(space, "-", linkpostTitle)
set newDraft to draftsPath & linkpostFilename & ".md"

set af to open for access file newDraft with write permission
set eof af to 0
tell application "Safari"
	activate
	set linkpostURLTitle to name of front document
	set linkpostUrl to URL of front document
	set linkpostQuote to (do JavaScript "(''+getSelection())" in document 1)
end tell
write "---" & return & "title: " & linkpostTitle & return & "date:" & return & "layout: post" & return & "categories: []" & return & "tags: []" & return & "link: " & linkpostUrl & return & "---" & return & return & "> " & linkpostQuote & return & "from: " & linkpostURLTitle to af as «class utf8»
close access af

tell application "Sublime Text"
	activate
	open newDraft
end tell

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

Can anyone help me with that? Thanks!

Edited by frevo
Link to comment

It would appear that the problem lies with the placement of your findReplace handler within the "Run NSAppleScript" action. Here's how it should look:

on alfred_script(q)
	set draftsPath to (path to home folder as text) & "jekyll:site:_drafts:"
	
	--set q to "This is a test draft"
	set linkpostTitle to q
	set linkpostFilename to findReplace(space, "-", linkpostTitle)
	set newDraft to draftsPath & linkpostFilename & ".md"
	
	set af to open for access file newDraft with write permission
	set eof af to 0
	tell application "Safari"
		activate
		set linkpostURLTitle to name of front document
		set linkpostUrl to URL of front document
		set linkpostQuote to (do JavaScript "(''+getSelection())" in document 1)
	end tell
	write "---" & return & "title: " & linkpostTitle & return & "date:" & return & "layout: post" & return & "categories: []" & return & "tags: []" & return & "link: " & linkpostUrl & return & "---" & return & return & "> " & linkpostQuote & return & "from: " & linkpostURLTitle to af as «class utf8»
	close access af
	
	tell application "Sublime Text"
		activate
		open newDraft
	end tell
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
Edited by smarg19
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...