I can't work out why this doesn't work

Hi Everyone
This is my first crack at Applescript.
It create an in folder and out folder. It watches the in folder for files, and if one is there
it’s suppose to run a photoshop action to convert it to Gif then move it into the out folder.
Now, everything works ok except when it get’s to the Photoshop bit, it doesn’t open the file so it stop working.
If I put the action bit of script into another script by it self it will work. It doesn’t make any sense.

tell application "Finder"
	make new folder at desktop with properties ¬
		{name:"120dpi-in", label index:6, comment:"Midland Typesetters Pty Ltd
		30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"}
	make new folder at desktop with properties ¬
		{name:"120dpi-out", label index:6, comment:"Midland Typesetters Pty Ltd
		30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"}
end tell
on open theFiles
	repeat with e in theFiles
		tell application "Adobe® Photoshop® 6.0"
			open e
			do script "ScaleTo120dpihigh"
		end tell
	end repeat
end open
-- the following procedure is executed periodically, to look for
-- files in the "In Basket"
-- the return value specifies the time interval (in ticks) between executions
on idle
	set inBoxPath to (path to desktop as string) & "120dpi-in:" -- path to "In Basket"
	set outBoxPath to (path to desktop as string) & "120dpi-out:" -- path to "Out Basket"
	set fileList to list folder inBoxPath -- list of files in "In Basket"
	set outList to list folder outBoxPath -- list of files already in "Out Basket"
	if (number of fileList) < 1 then return 5 --  nothing to do this time
	repeat with fileName in fileList
		set inFilePath to (inBoxPath & fileName)
		set outFile to (fileName & ".120dpi-out")
		if outFile is in outList then -- the file already exists so add unique id
			set outFile to (outFile & time of the (current date))
		end if
		set outFilePath to (outBoxPath & outFile)
		tell application "Finder"
			move file (fileName as string) of folder "120dpi-in" to folder "120dpi-out" with warning -- move file to "Out Basket"
		end tell
	end repeat
end idle

Thanks
Marcus

Hi, Marcus.

You’ve used two special kinds of handler here, one of which is probably not what you need.

An ‘open’ handler is usually found in a droplet. The code in it will normally only be executed if the script’s been saved as an application and files or folders have been dragged onto its icon. That’s probably why your Photoshop code isn’t having any effect.

An ‘idle’ handler is a feature of stay-open scripts - ie. scripts that have been saved as stay-open applications. The code in the handler is executed when the script’s icon is double-clicked, but the script doesn’t quit afterwards. Instead, it stays open and the operating system periodically sends it a signal to execute again. The number of seconds until the next signal is determined by a returned value at the end of the handler.

It’s 1:00 am here now and I’ve a busy day coming up, but hopefully you’ll be able to sort out the script yourself with the above information, or maybe someone else will chime in. Good luck with it. :slight_smile:

Thanks Nigel,
That all makes sence, but when I try and put the photoshop bit minus the open bit, inside the idle loop/handler thing,
It seems to work but it falls over when it trys to open the files in the In Folder.
when I edit the script it’s highlighted at the “open e” part, so I’m guesing I need to assign e as something which will open the files in the folder so that photoshop script will work properly, I think that’s the bit I don’t understand how to do…maybe?[/i]

It’s kinda late here but I can give a few pointers.

If you just moved the Photoshop processing lines from the open handler to the idle handler without changing, it won’t work.

If you put the lines in the idle handler repeat loop and changed e to fileName I bet you would have better success opening the files.

Idle handlers are timed, stay open scripts. At the end of the idle handler, before the “end idle”, add the line “return X” where X is the number of seconds to wait between running. When you save the script (you may have to “Save as…”) save as a stay open application.

You can combine commands to neaten your script. When you create the folders at the beginning of the script you can assign them to a variable:
set InBoxPath to (make new folder at desktop with properties ¬
{name:“120dpi-in”, label index:6, comment:“Midland Typesetters Pty Ltd
30 Hubble Street, Maryborough, Victoria www.midlandtypesetters.com.au”})

Use display dialog to check out what is in your variables
display dialog fileName
may show you ways to streamline your script. :slight_smile: Remember that aliases include the path: file “abc” of folder “def” of disk “ghi”

Good luck!

Hi Guys,
Me again,
I’ve done all that, but when I execute the script, it makes the folders and when I moved a .Tiff file into the 120dpi-in folder it errors out with “open fileName” so I thought I’d put the path in after the fileName that to error out saying “it expected an end of line”.
It looks to me that the “fileName” is already defined…so why doesn’t work??.
This is pull your hair out stuff, but I want to stick with it, even though I’m no dout enoying you guys no end. :wink:
Once this is done I’ll have to fined some books or a course or something, because there is heaps I want to do to be able to streamline my workflow.


tell application "Finder"
	
	set inBoxPath to make new folder at desktop with properties ¬
		{name:"120dpi-in", label index:6, comment:"Midland Typesetters Pty Ltd
		30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"}
	
	set outBoxPath to make new folder at desktop with properties ¬
		{name:"120dpi-out", label index:6, comment:"Midland Typesetters Pty Ltd
		30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"}
end tell

-- the following procedure is executed periodically, to look for
-- files in the "In Basket"
-- the return value specifies the time interval (in ticks) between executions
on idle
	set inBoxPath to (path to desktop as string) & "120dpi-in:" -- path to "In Basket"
	set outBoxPath to (path to desktop as string) & "120dpi-out:" -- path to "Out Basket"
	set fileList to list folder inBoxPath -- list of files in "In Basket"
	set outList to list folder outBoxPath -- list of files already in "Out Basket"
	if (number of fileList) < 1 then return 5 --  nothing to do this time
	repeat with fileName in fileList
		set inFilePath to (inBoxPath & fileName)
		set outFile to (fileName & ".120dpi-out")
		tell application "Adobe® Photoshop® 6.0"
		[color=yellow]open fileName[/color]	
			do script "ScaleTo120dpihigh"
		end tell
		
		if outFile is in outList then -- the file already exists so add unique id
			set outFile to (outFile & time of the (current date))
		end if
		display dialog fileName
		set outFilePath to (outBoxPath & outFile)
		tell application "Finder"
			move file (fileName as string) of folder "120dpi-in" to folder "120dpi-out" with warning -- move file to "Out Basket"
		end tell
	end repeat
	return 5
end idle

Thanks
Marcus

Hi Marcus,

Two things.

First - change this:

set fileList to list folder inBoxPath -- list of files in "In Basket" 
set outList to list folder outBoxPath -- list of files already in "Out Basket"

to this

set fileList to list folder inBoxPath without invisibles-- list of files in "In Basket" 
set outList to list folder outBoxPath without invisibles -- list of files already in "Out Basket"

You won’t get invisible files returned in your list that may cause Pshop to choke.

B)

Here you are telling Photoshop to open a file by it’s name. Earlier you listed the folder which returns item names. Then, you repeat with each fileName. You are close though because you are building the file to the path by declaring:

I would actually change that to:

 set inFilePath to (inBoxPath & fileName as string)

so you don’t end up with a list instead of a file path. Same with your outpath or any time you concatenate a file path for that matter.

Then, make this change:

And to further error proof your script, wrap your folder creation code with try - end try, in case the folder already exists.

Best regards,

Thanks Guys… but
It still errors out on


tell application "Adobe® Photoshop® 6.0" 
open inFilePath

AAARRGGHHH :?: :?: :?:

D’oh!
Photoshop 6 has only one command, “do script.” Use the Finder to open the file. Make sure that Photoshop is the default app for the images.

tell application "Finder"
	open alias fileName
end tell
tell application "Adobe® Photoshop® 6.0" 
    do script "ScaleTo120dpihigh" 
end tell 

And you probably need try blocks in there to catch errors and be sure that Photoshop doesn’t throw up a dialog on profile mismatches.

I guess I was too tired last night. :oops:

Sorry, guys now it falls over at
tell application “Finder”
open alias fileName
end tell
It’s like it can’t open the file…


tell application "Finder"
	try
		set inBoxPath to make new folder at desktop with properties ¬
			{name:"120dpi-in", label index:6, comment:"Midland Typesetters Pty Ltd
		30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"}
		
		set outBoxPath to make new folder at desktop with properties ¬
			{name:"120dpi-out", label index:6, comment:"Midland Typesetters Pty Ltd
		30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"}
	end try
end tell

-- the following procedure is executed periodically, to look for
-- files in the "In Basket"
-- the return value specifies the time interval (in ticks) between executions
on idle
	set inBoxPath to (path to desktop as string) & "120dpi-in:" -- path to "In Basket"
	set outBoxPath to (path to desktop as string) & "120dpi-out:" -- path to "Out Basket"
	set fileList to list folder inBoxPath without invisibles -- list of files in "In Basket"
	set outList to list folder outBoxPath without invisibles -- list of files already in "Out Basket"
	if (number of fileList) < 1 then return 5 --  nothing to do this time
	repeat with fileName in fileList
		set inFilePath to (inBoxPath & fileName as string)
		set outFile to (fileName & ".120dpi-out")
		tell application "Finder"
			open alias fileName
		end tell
		tell application "Adobe® Photoshop® 6.0"
			do script "ScaleTo120dpihigh"
		end tell
		if outFile is in outList then -- the file already exists so add unique id
			set outFile to (outFile & time of the (current date))
		end if
		display dialog fileName
		set outFilePath to (outBoxPath & outFile)
		tell application "Finder"
			move file (fileName as string) of folder "120dpi-in" to folder "120dpi-out" with warning -- move file to "Out Basket"
		end tell
	end repeat
	return 5
end idle

Sorry again for the hassle

Hi, Marcus.

As Mytzlscript pointed out earlier, you can’t open a file with just its name. You have to include where it is on which disk. However, the example given of how to open a file in Photoshop was incomplete, as it failed to turn the path string into a file specification. It should have been:

tell application "Adobe® Photoshop® 6.0"
  open file inFilePath -- NB. 'file'
end tell

I say “should have been”. It works with my copy of Photoshop Elements so I assume it also works with the full application. ‘Open’ is one of the commands that even unscriptable applications are supposed to support. They’re not required to support a ‘save’ from AppleScript though, so your Photoshop script “ScaleTo120dpihigh” will have to take care of resaving the file before the AppleScript script moves it.

Here’s an advancement of your script, which also includes the renaming of the file prior to moving it. Your original ‘move’ line contained ‘with warning’, which isn’t an option in OS 8/9.

tell application "Finder"
  try
    make new folder at desktop with properties ¬
      {name:"120dpi-in", label index:6, comment:"Midland Typesetters Pty Ltd
      30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"}
  end try
  try
    make new folder at desktop with properties ¬
      {name:"120dpi-out", label index:6, comment:"Midland Typesetters Pty Ltd
      30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"}
  end try
end tell

-- the following procedure is executed periodically, to look for
-- files in the "In Basket"
-- the return value specifies the time interval (in ticks) between executions
on idle
  set inBoxPath to (path to desktop as string) & "120dpi-in:" -- path to "In Basket"
  set outBoxPath to (path to desktop as string) & "120dpi-out:" -- path to "Out Basket"
  set fileList to list folder inBoxPath without invisibles -- list of files in "In Basket"
  set outList to list folder outBoxPath without invisibles -- list of files already in "Out Basket"
  if fileList is {} then return 5 --  nothing to do this time
  
  repeat with i from 1 to (count fileList)
    set fileName to item i of fileList
    tell application "Adobe® Photoshop® 6.0"
      open file (inBoxPath & fileName)
      do script "ScaleTo120dpihigh"
    end tell
    set outFile to (fileName & ".120dpi-out")
    if outFile is in outList then -- the file already exists so add unique id
      set outFile to (outFile & time of the (current date))
    end if
    tell application "Finder"
      set name of file fileName of folder inBoxPath to outFile -- rename the file
      move file outFile of folder inBoxPath to folder outBoxPath -- move file to "Out Basket"
    end tell
  end repeat
  
  return 5
end idle

Good point Nigel, always a good idea - however it it does work for me to tell Phsop to open a file defined only as a string. I am successfully testing this under OS 9.2.2 - Photoshop 5.5 - AS v1.8.3

Marcus, once you get this running, one other thing I would do is move inBoxPath and outBoxPath out of the on Idle handler. No need to waste time setting that on every cycle when you could have them be persistent properties.


property inBoxPath : (path to desktop) & "120dpi-in:" as string -- path to "In Basket" 
property outBoxPath : (path to desktop) & "120dpi-out:"  as string-- path to "Out Basket" 

tell application "Finder" 
  try 
    make new folder at desktop with properties ¬ 
      {name:"120dpi-in", label index:6, comment:"Midland Typesetters Pty Ltd 
      30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"} 
  end try 
  try 
    make new folder at desktop with properties ¬ 
      {name:"120dpi-out", label index:6, comment:"Midland Typesetters Pty Ltd 
      30 Hubble Street, Maryborough, Victoria  [url=http://www.midlandtypesetters.com.au]www.midlandtypesetters.com.au[/url]"} 
  end try 
end tell 

-- the following procedure is executed periodically, to look for 
-- files in the "In Basket" 
-- the return value specifies the time interval (in ticks) between executions 
on idle 
  set fileList to list folder inBoxPath without invisibles -- list of files in "In Basket" 
  set outList to list folder outBoxPath without invisibles -- list of files already in "Out Basket" 
  if fileList is {} then return 5 --  nothing to do this time 
  
  repeat with i from 1 to (count fileList) 
    set fileName to item i of fileList 
    tell application "Adobe® Photoshop® 6.0" 
      open file (inBoxPath & fileName as string) 
      do script "ScaleTo120dpihigh" 
    end tell 
    set outFile to (fileName & ".120dpi-out") 
    if outFile is in outList then -- the file already exists so add unique id 
      set outFile to (outFile & time of the (current date)) 
    end if 
    tell application "Finder" 
      set name of file fileName of folder inBoxPath to outFile -- rename the file 
      move file outFile of folder inBoxPath to folder outBoxPath -- move file to "Out Basket" 
    end tell 
  end repeat 
  
  return 5 
end idle

Do you have Jon’s Commands installed, by any chance? That provides automatic string-to-file-specification coercions in the background - or, at least, the pre-X versions did. It was always causing problems for people on the AppleScript-Users and MACSCRPT mailing lists, who couldn’t work out why their scripts wouldn’t work on anyone else’s machines. :slight_smile: In the end, with Jon’s reluctant guidance, I used ResEdit to neuter my own copy. :wink:

There may also have been another OSAX that did something similar, but I can’t remember now. Akua Sweets, perhaps?

I have both Akua and Jon’s - but tested it without them in the Additions folder - checked Script DeBugger’s manifest, compiled and ran in Apple’s Script Editor. I must confess to being a little ignorant about why the string to file coercion works without any osax - perhaps AS version? I know I can’t get away with that in any of the scripts I’m writing under Panther and still agree with you that it is just good practice to turn the path string into a file spec.

Sorry if I confused anyone. Hope your script is working Marcus.

Okay, this totally illuminates my ignorance. I rebooted with only standard additions and saw that it does not work. I thought simply quitting my script editor -scaling back to standard additions - then restarting my editor would do it.

Lesson learned. Thanks Nigel. I’ll keep that in mind before I post.

:smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley:

Thanks, (all of you) guys.
You’ve really saved my but…
One more small question?

What’s a good Book to get to learn from, that’s also written so a five year old can understand. Or any online course which are good???

Thanks again
Marcus

That’s what made the problem so obscure. Jon’s didn’t so much do the coercions - in the same way that it did its commands - as install them. Whatever happened to the OSAX itself, the coercions remained installed until the next restart. Very difficult to discover if you didn’t already know about them!

Everyone seems to be raving about “AppleScript: The Definitive Guide” by Matt Neuburg. I haven’t seen it myself, so I don’t know if it’s suitable for a five-year-old. :wink:

Bill Briggs’s popular series of articles is still available on MacCentral at http://maccentral.macworld.com/features/applescriptprimer00/

Now I know, and knowing is half the battle.