"No such file or directory" - but it's there

I’m trying to write an AppleScript that includes a “do shell script” command. The script is intended to work on selected files on the desktop, reading a date from each file and then using the file contents to make a new entry in the Day One journal program using the date. The form of the Day One CLI is this:

dayone -d=“” new < ~/Desktop/

Unfortunately, however I write this script, I get an error:

error “Finder got an error: sh: ~/Desktop/11-9.txt: No such file or directory” number 1

(My test files are currently called 11-9.txt and 11-10.txt, but I’ve also experienced the problem using other file names.)

Here are the scripts I’ve tried. At first, I named the files with the date (e.g., “November 9, 2014”), which the CLI can interpret. The following script yields the error above in the ScriptEditor Result window.

tell application "Finder"
	activate
	set theseItems to selection
	repeat with thisItem in theseItems
		set thePath to thisItem as string
		set theAlias to thisItem as alias
		set theDate to name of theAlias
		do shell script "dayone -d=" & quoted form of theDate & " new < \"~/Desktop/" & theDate & "\""
	end repeat
end tell

The following variation brings up a ScriptEditor alert:

Finder got an error: sh: :Users:john:Desktop:11-9.txt: No such file or directory

And when I tried another tack, reading the date (in form “11/09/2014”) from the top of the file contents, I got the same error as just above:

tell application "Finder"
	activate
	set theseItems to selection
end tell
repeat with thisItem in theseItems
	tell application "Finder"
		set thePath to thisItem as string
		set fp to open for access file thePath
		set theDate to read fp until "4"
		close access fp
	end tell
	do shell script "dayone -d=" & quoted form of theDate & " new < " & quoted form of thePath
end repeat

Why is my script not finding the files? What am I missing?

TIA.

Model: MacBook Pro (early 2011)
AppleScript: 2.4
Browser: Safari 600.1.25
Operating System: Mac OS X (10.8)

Hi,

I’d read the whole file contents with AppleScript and separate the date from the contents, like this


tell application "Finder"
	activate
	set theseItems to selection
end tell

repeat with thisItem in theseItems
	set fileContent to read file (thisItem as text)
	set theDate to text 1 thru 10 of fileContent
	set theText to text 11 thru -1 of fileContent
	do shell script "dayone -d=" & quoted form of theDate & " new " & quoted form of theText
end repeat


Oddly enough, that script returns an alert:

Script Error
sh: dayone: command not found

The dayone command does run correctly when it’s manually entered in Terminal.

try to specify the full path to the CLI in the do shell script line

When I specify the full path to the dayone command this way:

	do shell script "/usr/local/bin/dayone -d=" & quoted form of theDate & " new " & quoted form of theText

I get this error:

error “sh: -c: line 0: unexpected EOF while looking for matching `‘’
sh: -c: line 1: syntax error: unexpected end of file” number 2

Hi John,

Have you tried just reading the file and checking the result:

set f to choose file
read f

I don’t have dayone, but was thinking that maybe there’s a header or something that might cause the EOF error.

gl,
kel

Kel,

It seems that the problem I have isn’t in reading the file. When I add “display dialog” commands to my scripts above, the contents of the file show just fine. But as you can see, the errors I’m getting are prefixed with “sh:”, which says to me that the problem occurs during the shell interaction.

Thanks,
John

Hi.

Looking at Day One’s cli manual, it could be that literal text has to come in through standard input:

do shell script "echo " & quoted form of theText & " | /usr/local/bin/dayone -d=" & quoted form of theDate & " new"

Or maybe:

do shell script "/usr/local/bin/dayone -d=" & quoted form of theDate & " new <<<" & quoted form of theText

Edit: In fact the manual does say “The entry text is read from stdin.”

Nigel, thanks for keeping this thread alive. I’m sure this problem can be solved!

The CLI manual you refer to shows this as an example, which I’m confirmed works, and which doesn’t seem to use stdln:

dayone -d=“03/03/2011 5:30PM” -s=true new < ~/Desktop/note.txt

Creates an entry with a specified creation date. The contents of note.txt are used as the entry text. Additionally, the entry is starred.

My problem has always been that the shell, when executed from do shell script, doesn’t seem to think the file exists. If I enter the command directly in Terminal, it works.

Hi,

Try putting parenthesis around:
(theDate & " new")

Edited: no. around the other part:
(quoted form of theDate) & " new "

Yes. The “<” causes the text to be read from a file called “note.txt” on the desktop.

Stefan’s suggestion in post #2 was to use ‘read’ to get the entire text (assumed to consist only of single-byte characters) from the file, extract the date (assumed to be the first ten characters), and feed the pass of the text (without the date) to the shell script. In this case, the text would have to be fed in via stdin because it’s not coming directly from the file.

One problem with your second script in post #1 is that the value of ‘thePath’ is an HFS path, whereas the shell script requires a POSIX path. The script would be better like this (although I’m not able to test it):

tell application "Finder"
	activate
	set theseItems to selection
end tell

repeat with thisItem in theseItems
	tell application "Finder"
		set theAlias to thisItem as alias
	end tell
	
	set theDate to (read theAlias from 1 for 10) -- Assuming the file contains text consisting of only single-byte characters and beginning with a date in the form "xx/xx/xxxx".
	
	do shell script "dayone -d=" & quoted form of theDate & " new < " & quoted form of POSIX path of theAlias
end repeat