Folder action to prompt for a new filename and then rename the file

I cant work out how to get the script to rename with prompt to enter a new filename and add .ps extention at the end and pass this file on to instant pdf. So Instant pdf sees the file as $newfilename.ps,

I would appreciate if you could help me work out how script below:
Im a complete noob in scripting, but over the last couple of days I have produced this script to automate a print spool folder from cups file:/ (spool folder) The FileDevice in Cups produces the same fixed filename every time URI. file:/filename.ps and i want it to be changed by the script by prompting for a new name, but cant work it out

your expertise will be appreciated, my script so far:

[b]on adding folder items to theFolder after receiving theAddedItems
try

	repeat with thisItem in theAddedItems
		set FilePath to quoted form of POSIX path of (contents of thisItem)
		(do shell script "chmod -R 777 " & FilePath with administrator privileges)

(rename with prompt bit to go in here ? )

		tell application "Instant PDF"
			activate (open thisItem)
		end tell
		
		tell application "Finder"
			delete theAddedItems
		end tell
	end repeat
	
on error
	tell application "Finder"
		activate (display dialog "Problem setting the permissions please try manually" buttons {"OK"} default button 1)
	end tell
end try

end adding folder items to[/b]

Well, I did not quite understand your explication. Do you want to just rename the added files and then open them with the application “Instant PDF”, then add the script below and your folder to the “Folder-Actions” application.

The script:

on adding folder items to theFolder after receiving theObjects
	repeat with i in theObjects
		try
			-- asking for new file name
			set myNewFileName to text returned of (display dialog "Please enter a new file name:" default answer "")
			
			-- adding ".ps" extension
			if myNewFileName does not end with ".ps" then set myNewFileName to (myNewFileName & ".ps") as string
			
			-- rename the file
			tell application "Finder" to set name of i to myNewFileName
			
			-- open the file in "Instant PDF"
			tell application "Instant PDF" to open i
		on error theMsg
			-- on error, display error message
			set myPOSIX to (POSIX path of i)
			display dialog ("File \"" & myPOSIX & "\" couldn't be renamed:" & return & return & theMsg) buttons "OK" default button 1
		end try
	end repeat
end adding folder items to

Thanks, I’ll give that a go… Filedevice in CUPS makes a file with SYSTEM ownership, and it throws an error, saying instant pdf does not have sufficient priveledges. The privelges have to be changed before finder can rename it or instant pdf can open.

it needs the (do shell script "chmod -R 777 " & FilePath with administrator privileges) somewhere

and the delete all bit at the end to delete the contents of the folder

how would the script be then ?

All right, I don’t really understand you. So what is it exactly what you want to do?

You want to print the document to a pdf-file into a folder with CUPS. Then, when the file is saved in that folder, rename/convert it to a PostScript-file and open it with Instant PDF. Am I right?

Everything worked fine until we ungraded to snow leopard and then the virtual printers did not work anymore.
due to apple changing the security settings. Enfocus say that an update will be available in a few months to get them working again.

the script I made fixed problem, except change the name. Every file coming out of instant pdf was called file.pdf this meant I had to change every file manually to a unique name after processing.

What I need, is your rename code injecting in to my current script and it will be the excellent, and very arreciated

  1. ps file goes in to folder.
  2. change permissions of file to 777
  3. rename file with promt (enter new filename click ok)
  4. open the new renamed file in instant pdf (that does its own scripts)
  5. delete the file from the folder

my script & your script so you understand what it needs to do

on adding folder items to theFolder after receiving theAddedItems
    try
        
        repeat with thisItem in theAddedItems
            set FilePath to quoted form of POSIX path of (contents of thisItem)
            (do shell script "chmod -R 777 " & FilePath with administrator privileges)
            
          -- asking for new file name
                     
           -- adding ".ps" extension
                     
           -- rename the file
          
            tell application "Instant PDF"
                activate (open thisItem)
            end tell
            
            tell application "Finder"
                delete theAddedItems
            end tell
        end repeat
        
    on error
        tell application "Finder"
            activate (display dialog "Problem setting the permissions please try manually" buttons {"OK"} default button 1)
        end tell
    end try
    
end adding folder items to

All right, I understand now. And the script below doesn’t work?

(* 1. FILE GOES INTO FOLDER *)
on adding folder items to theFolder after receiving theAddedItems
	try
		repeat with thisItem in theAddedItems
			
			(* 2. Change permissions *)
			set FilePath to quoted form of (POSIX path of thisItem)
			(do shell script "chmod -R 777 " & FilePath with administrator privileges)
			
			(* 3. RENAME THE ADDED FILE *)
			-- asking for new file name
			set NewName to (display dialog ("Enter a new name for: \"" & (POSIX path of thisItem) & "\"") as string default answer "")
			
			-- adding ".ps" extension
			if NewName does not end with ".ps" then set NewName to (NewName & ".ps") as string
			
			-- rename the file
			tell application "Finder" to set name of thisItem to NewName
			
			-- get renamed file
			set thisItem to file NewName of theFolder
			
			(* 4. OPEN FILE IN INSTANT PDF *)
			-- open renamed file
			tell application "Instant PDF"
				activate (open thisItem)
			end tell
			
			(* 5. DELETE THE ADDED FILE *)
			-- delete Added Items
			tell application "Finder"
				delete theAddedItems
			end tell
		end repeat
		
	on error
		tell application "Finder"
			activate (display dialog "Problem setting the permissions please try manually" buttons {"OK"} default button 1)
		end tell
	end try
	
end adding folder items to

PS: Another thing I noticed is your use of the do shell script-command. I really don’t now how the terminal works at all. But maybe the POSIX path doesn’t have to be quoted, instead, you should leave it and just pleace a '' before every space. Then you would get this:

(* 1. FILE GOES INTO FOLDER *)
on adding folder items to theFolder after receiving theAddedItems
	try
		repeat with thisItem in theAddedItems
			
			(* 2. Change permissions *)
			set FilePath to (POSIX path of thisItem)
			set FilePath to ConvertName(FilePath)
			(do shell script "chmod -R 777 " & FilePath with administrator privileges)
			
			(* 3. RENAME THE ADDED FILE *)
			-- asking for new file name
			set NewName to (display dialog ("Enter a new name for: \"" & (POSIX path of thisItem) & "\"") as string default answer "")
			
			-- adding ".ps" extension
			if NewName does not end with ".ps" then set NewName to (NewName & ".ps") as string
			
			-- rename the file
			tell application "Finder" to set name of thisItem to NewName
			
			-- get renamed file
			set thisItem to file NewName of theFolder
			
			(* 4. OPEN FILE IN INSTANT PDF *)
			-- open renamed file
			tell application "Instant PDF"
				activate (open thisItem)
			end tell
			
			(* 5. DELETE THE ADDED FILE *)
			-- delete Added Items
			tell application "Finder"
				delete theAddedItems
			end tell
		end repeat
		
	on error
		tell application "Finder"
			activate (display dialog "Problem setting the permissions please try manually" buttons {"OK"} default button 1)
		end tell
	end try
	
end adding folder items to


(* ===== HANDLERS ===== *)
on ConvertName(PosixPathString)
	set NewName to {}
	repeat with i in PosixPathString
		if (i as string) is " " then set end of NewName to "\\ "
		if (i as string) is not " " then set end of NewName to (i as string)
	end repeat
	
	set NewName to NewName as string
	return NewName
end ConvertName

I hope it works,
ief2

Hi M8

Neither worked, I get to entering the name and click OK jumped to the error handler

HI,

it might be useful to display the error description


.
on error e number n
	tell application "Finder"
		display dialog "Problem setting the permissions please try manually" & return ¬
			& "error: " & e & "(" & (n as text) & ")" buttons {"OK"} default button 1
	end tell
end try
.

Many Thanks StefanK

Ah yes, 2 thing are happening now,

dialogue box
error cant get file, renamed.ps of alias Machintosh HD :enftmp:.(-1728)

the file is physically renamed in the folder meaning the folder sees it as
a new file and repeats the routine.
Loop - Loop

it only need to run once per file, then the file is delete

this is the script

(* 1. FILE GOES INTO FOLDER *)
on adding folder items to theFolder after receiving theAddedItems
	try
		repeat with thisItem in theAddedItems
			
			(* 2. Change permissions *)
			set FilePath to quoted form of (POSIX path of thisItem)
			(do shell script "chmod -R 777 " & FilePath with administrator privileges)
			
			(* 3. RENAME THE ADDED FILE *)
			-- asking for new file name
			-- asking for new file name
			set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")
			
			-- adding ".ps" extension
			if NewName does not end with ".ps" then set NewName to (NewName & ".ps") as string
			
			-- rename the file
			tell application "Finder" to set name of thisItem to NewName
			
			-- get renamed file
			set thisItem to file NewName of theFolder
			
			(* 4. OPEN FILE IN INSTANT PDF *)
			-- open renamed file
			tell application "Instant PDF"
				activate (open thisItem)
			end tell
			
			(* 5. DELETE THE ADDED FILE *)
			-- delete Added Items
			tell application "Finder"
				delete theAddedItems
			end tell
		end repeat
		
		
	on error e number n
		tell application "Finder"
			display dialog "Problem setting the permissions please try manually" & return ¬
				& "error: " & e & "(" & (n as text) & ")" buttons {"OK"} default button 1
		end tell
	end try
	
	
end adding folder items to

.
set thisItem to file NewName of theFolder
.

cannot work without a Finder tell block

try this


.
-- rename the file
tell application "Finder"
	set name of contents of thisItem to NewName
	
	-- get renamed file
	set thisItem to file NewName of theFolder
end tell

(* 4. OPEN FILE IN INSTANT PDF *)
-- open renamed file
tell application "Instant PDF"
	open (thisItem as alias)
end tell
.

the infinite loop is a common problem, the event handler treats a renamed file as a new added file.
A solution is to move the file into another folder and rename it there

still loops, thanks for your help though, im learning applescript as I go along

all the files created by cups virtual printer have identical filenames at first sent to the folder are all called “file.ps”
(hence why I need to rename them)

is it possible to have the folder action check at the beginning to only process files called “file.ps”

if the filename is not file.ps do not process ? this way would it prevent the renamed file being picked up again and looped

Of course


on adding folder items to theFolder after receiving theAddedItems
	try
		repeat with thisItem in theAddedItems
			if (name of (info for thisItem) is "file.ps") then
				(* 2. Change permissions *)
				set FilePath to quoted form of (POSIX path of thisItem)
				(do shell script "chmod -R 777 " & FilePath with administrator privileges)
				
				set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")
				if NewName does not end with ".ps" then set NewName to (NewName & ".ps")
				tell application "Finder"
					set name of contents of thisItem to NewName
					set thisItem to file NewName of theFolder
				end tell
				tell application "Instant PDF"
					open (thisItem as alias)
				end tell
			end if
		end repeat
		
	on error e number n
		tell application "Finder"
			display dialog "Problem setting the permissions please try manually" & return ¬
				& "error: " & e & "(" & (n as text) & ")" buttons {"OK"} default button 1
		end tell
	end try
end adding folder items to

Thats it ! just added the delete file bit at the end and it problem solved, Many thanks , yipee !