error on close (47)

I have a script that reads filenames from a textfile, looks for them and moves the files
Running it as a script works just fine but when I save it as an application it gives me an error “47 does not understand the close message”

  • I have a Dutch operating system so the translation might not be exact.
    I guess it has something to do with the close of the chosen textfile

Anyone any ideas?

Peter


property removedImages : "/Volumes/HD1/Productgroepen_REMOVED/Winkel_UK"
property logFile : "HD1:Productgroepen_REMOVED:" & "log_Winkel_UK.txt"

set chosenfolder to choose folder with prompt "Choose a Folder" without invisibles
set chosenPOSIXfolder to quoted form of POSIX path of chosenfolder

set chosenfile to (choose file) as Unicode text

--set ProgressBar to load script alias (((path to scripts folder) as text) & "Lib:BP Progress Bar Controller.scpt")
set ProgressBar to load script alias (((path to me as text) & "Contents:Resources:BP Progress Bar Controller.scpt")) --for application bundle
tell ProgressBar
	initialize("Move Images Phase One")
	barberPole(true)
	setStatusTop to "Moving Images with Status 'Winkel UK'"
	setStatusBottom to "Only a moment..."
end tell

try
	set openfile to open for access chosenfile
	set filecont to read openfile
	close openfile
on error errmsg number errnum
	try
		close access openfile
	end try
	error errmsg number errnum
end try

set filenames to paragraphs of filecont

repeat with filename in filenames
	if length of filename is greater than 0 then
		
		tell application "Finder"
			try
				set folderPOSIXpath to quoted form of POSIX path of removedImages
				set folderalias to removedImages as alias
			on error
				do shell script "/bin/mkdir -p " & folderPOSIXpath
				try
					do shell script ("/usr/bin/find " & chosenPOSIXfolder & " -name " & filename & " -type f -exec mv {} " & quoted form of removedImages & " \\;")
				end try
			end try
		end tell
	end if
end repeat

tell ProgressBar to quit

--set ProgressBar to load script alias (((path to scripts folder) as text) & "Lib:BP Progress Bar Controller.scpt")
set ProgressBar to load script alias (((path to me as text) & "Contents:Resources:BP Progress Bar Controller.scpt")) --for application bundle
tell ProgressBar
	initialize("Move Images Phase Two")
	barberPole(true)
	setStatusTop to "Collecting File Info for Log"
	setStatusBottom to "Only a moment..."
end tell

Write_Log from (do shell script "/bin/echo `date +%d-%m-%Y'     '%H:%M:%S`")

tell application "Finder" to set removedfiles to files of folder "HD1:Productgroepen_REMOVED:Winkel_UK:"
repeat with aFile in removedfiles
	set {name:filename, creation date:file_creation_date} to info for (aFile as alias)
	Write_Log from ("Name: " & filename & "\t\t" & " Creation Date: " & file_creation_date)
end repeat
end

tell ProgressBar to quit

tell application (path to frontmost application as Unicode text)
	with timeout of 36000 seconds
		display dialog "Files are moved" buttons {"OK"} default button 1
	end timeout
end tell

on Write_Log from theMessage
	try
		set the logStream to open for access file logFile with write permission
		set logFileEof to get eof of the logStream
		write theMessage & return to logStream starting at eof as «class utf8»
		close access logStream
		return true
	on error
		try
			close access file logFile
		end try
		return false
	end try
end Write_Log

Hi,

there’s a syntax error anyway in this part


.
set chosenfile to (choose file) as Unicode text
.
try
   set openfile to open for access chosenfile
   set filecont to read openfile
   close openfile
on error errmsg number errnum
   try
       close access openfile
   end try
.

if you specify the file as a path string, you have to add file, if it’s an alias you can omit it
either


.
set chosenfile to (choose file) as Unicode text
.
try
   set openfile to open for access file chosenfile
.

or


.
set chosenfile to (choose file)
.
try
   set openfile to open for access chosenfile
.

is correct.

But in your case, you don’t need open for access at all,
the following is sufficient, all lines are commented out, which are not needed


set chosenfile to (choose file)

.

-- try
--  set openfile to open for access chosenfile
   set filecont to read chosenfile
--   close openfile
-- on error errmsg number errnum
--   try
--       close access openfile
--   end try
--   error errmsg number errnum
-- end try

Hi stefan.

When I use

set chosenfile to (choose file)
set filecont to read chosenfile
set filenames to paragraphs of filecont

nothing happens - it need to be opened as Unicode text

set chosenfile to (choose file) as Unicode text
set filecont to read chosenfile
set filenames to paragraphs of filecont

I get an error cannot make file

One thing I still dont understand: Why does my first script run ok as a regular script but not as an application “47 does not understand the close message”

weird, on my machine (and as expected) the first form works fine and the seconds causes an error
Can’t make “Path:ToFile” into type file.

I don’t dont the reason of your error message, but I guess it’s some syntax error, which will be tolerated as script but not as application

Stefan,

What I mean by “nothing happens” is that it opens the file without errors but there is no output of the read content

event:

choose file
		alias "HD2:Users:administrator:Desktop:remove.txt"
	read alias "HD2:Users:administrator:Desktop:remove.txt"
		"\ny"

But it should, in your log there is a line feed character and a “y”

Hi Stefan,

The text file itself was the problem.
I gave a return after the last line and now it works as a regular script
Running my script as a prgram gives me an error “can’t get end”
Would you be so kind to have a look at my script:


property removedImages : "/Volumes/HD1/Productgroepen_REMOVED/Winkel_UK"
property logFile : "HD1:Productgroepen_REMOVED:" & "log_Winkel_UK.txt"

set chosenfolder to choose folder with prompt "Choose a Folder" without invisibles
set chosenPOSIXfolder to quoted form of POSIX path of chosenfolder

set chosenfile to (choose file)
set filecont to read chosenfile
set filenames to paragraphs of filecont


set ProgressBar to load script alias (((path to scripts folder) as text) & "Lib:BP Progress Bar Controller.scpt")
--set ProgressBar to load script alias (((path to me as text) & "Contents:Resources:BP Progress Bar Controller.scpt")) --for application bundle
tell ProgressBar
	initialize("Move Images Phase One")
	barberPole(true)
	setStatusTop to "Moving Images with Status 'Winkel UK'"
	setStatusBottom to "Only a moment..."
end tell

repeat with filename in filenames
	if length of filename is greater than 0 then
		
		tell application "Finder"
			try
				set folderPOSIXpath to quoted form of POSIX path of removedImages
				set folderalias to removedImages as alias
			on error
				do shell script "/bin/mkdir -p " & folderPOSIXpath
				try
					do shell script ("/usr/bin/find " & chosenPOSIXfolder & " -name " & filename & " -type f -exec mv {} " & quoted form of removedImages & " \\;")
				end try
			end try
		end tell
	end if
end repeat

tell ProgressBar to quit

set ProgressBar to load script alias (((path to scripts folder) as text) & "Lib:BP Progress Bar Controller.scpt")
--set ProgressBar to load script alias (((path to me as text) & "Contents:Resources:BP Progress Bar Controller.scpt")) --for application bundle
tell ProgressBar
	initialize("Move Images Phase Two")
	barberPole(true)
	setStatusTop to "Collecting File Info for Log"
	setStatusBottom to "Only a moment..."
end tell

Write_Log from (do shell script "/bin/echo `date +%d-%m-%Y'     '%H:%M:%S`")

tell application "Finder" to set removedfiles to files of folder "HD1:Productgroepen_REMOVED:Winkel_UK:"
repeat with aFile in removedfiles
	set {name:filename, creation date:file_creation_date} to info for (aFile as alias)
	Write_Log from ("Name: " & filename & "\t\t" & " Creation Date: " & file_creation_date)
end repeat
end

tell ProgressBar to quit

tell application (path to frontmost application as Unicode text)
	with timeout of 36000 seconds
		display dialog "Files are moved" buttons {"OK"} default button 1
	end timeout
end tell

on Write_Log from theMessage
	try
		set the logStream to open for access file logFile with write permission
		set logFileEof to get eof of the logStream
		write theMessage & return to logStream starting at eof as «class utf8»
		close access logStream
		return true
	on error
		try
			close access file logFile
		end try
		return false
	end try
end Write_Log
end