Syntax broken migrating from OS9 to OS 10

I too am struggling with the migration from OS 9 to X. The first two lines of the first script I’ve tried to run are broken, and I bet a lot of the rest of them are, too. I would like to be able to run this while in Column View. The View seems to make no difference, but please advise me how to fix this for OS X, and whether different views require different syntax. Thanks for any help you can give!

In plain english, I will have a folder full of files with very similar names: pic001.jpg, pic002.jpg, DSCN001.jpg, DSCN002.jpg, etc. You know what I mean, right? I’d like to “select,” or highlight, one of them; use the dialog box to truncate its name; have AS create a folder named with the remaining text and gather all the files in the “series” into the new folder. Used to work fine, now it’s broke! (There’s no way to know ahead of time exactly what naming conventions will be used. Hence, I’ve found I can’t use a set amount of truncation; I pretty much always have to do it by hand.)

I’m not sure I’ll be using the “set position” and size part of this script. That’s a legacy from attempting to emulate the OS X Column View in OS 9. (I always liked the NexT screen shots, and have wanted Column Views for a LONG time!) But, the rest of this thing would still be very handy! Thanks immensely for any help and other tips.

tell application “Finder”
activate
set theTargetFolder to container of selection
set theFileName to name of selection as string
if last word of theFileName contains “jpg” then
set theFileName to (characters 1 thru ((length of theFileName) - 4)) of theFileName as text
end if
display dialog “Delete the characters you don’t want to use:” default answer theFileName
set theNewFolderNameTemp to the text returned of result
set theFiles to every file of theTargetFolder whose name contains theNewFolderNameTemp
set theNewFolderName to theNewFolderNameTemp & “.”

set theNewFolder to make new folder at theTargetFolder ¬
	with properties {name:theNewFolderName}
open theNewFolder
set position of front window to {575, 65}
set size of front window to {252, 539}
set current view of front window to (name)
set «class cuss» of front window to false
close theNewFolder

move theFiles to theNewFolder

end tell

Hi,

Yes list view options is a little tricky in OSX. I rewrote some that you can use if you want:

tell application “Finder”
activate
set the_sel to (selection)
try
set sel_item to (first item of the_sel)
on error
return
end try
set theTargetFolder to (container of sel_item)
set theFileName to (name of sel_item)
set the_ext to (name extension of sel_item)
if not (the_ext is “pdf”) then return
if (theFileName ends with the_ext) then
set theFileName to (text 1 thru -5 of theFileName)
end if
– error checked until here
display dialog “Delete the characters you don’t want to use:” default answer theFileName
set theNewFolderNameTemp to the text returned of result
set theFiles to every file of theTargetFolder whose name contains theNewFolderNameTemp
set theNewFolderName to theNewFolderNameTemp & “.”

set theNewFolder to make new folder at theTargetFolder ¬
	with properties {name:theNewFolderName}
open theNewFolder
tell front window
	set current view to list view
	set position to {575, 65}
	set bounds to {575, 65, 575 + 252, 65 + 539}
end tell
set sort column of list view options of front window to name column
--set «class cuss» of front window to false
close front window

move theFiles to theNewFolder

end tell

editted: you don’t need the line:

set position to {575, 65}

because it is in the bounds.

gl,

Dear kel,
Thanks immensely for your help. I’ll try that out tonight.

I wonder how you know the new syntax. I, along with the Venter, am frustrated by the seemingly total lack of documentation on how exactly to write AppleScripts in OS X. Any pointers to online materials would be greatly appreciated! Thanks again.

Hi BuckyE,

What I did was look at your script line by line and see what happens. I started with this:

tell application “Finder”
activate
set theTargetFolder to container of selection
end tell

I already knew that there has always been issues with things like ‘selection’, ‘every element’, etc. so I break it up. Sometimes just adding parenthesis works.

tell application “Finder”
activate
set theTargetFolder to container of (selection)
end tell

but this doesn’t work. So seperate it:

tell application “Finder”
activate
set the_sel to (selection)
set theTargetFolder to container of the_sel
end tell

Here the error is that it can’t get container of a list, so get container of an item:

tell application “Finder”
activate
set the_sel to (selection)
set theTargetFolder to container of first item of the_sel
end tell

This works so you can now go change things and go on with the rest of the script.

Using the finder was mainly dictionary usage. One part that gives trouble to people is the list view options. You go from ‘Finder window’ to ‘list view options’ to ‘sort column’ to ‘column’ to ‘name’. These objects and properties contain reserved words used in other parts of the Finder and Finder objects so I try to use it out of a tell block like ‘tell front window’. From experience experimenting with this property, the programmers made an error writing this ‘list view options’ part. Just using different combinations you can get the correct syntax.

I learned this stuff by reading the AppleScriptLanguageGuide.PDF a lot, ScriptingAdditions.PDF, AppleScriptFinderGuide.PDF, and other manuals. Some parts are outdated and needs experimenting. More advanced stuff can be learned:

http://www.applescriptsourcebook.com/home.html

The tips and reviews are good although a little dated at the moment.

Lastly, you can learn a lot of debugging by trying to come up with answers to people’s questions at bbs like here. It takes some work but it gets easier going from different OS to OS. Apple always fixes a few old bugs and comes up with a lot of new ones.

gl,

Dear Friends,
I am indeed learning by debugging, except right now it’s my own scripts! I am finding that the command “open” is flaky at best, at least the way I’m trying to use it, especially in column view, but even in list view. I recorded opening a window by double-clicking a folder in a column view window, and the Script Editor wrote:

make new Finder window to the_Folder (where the_Folder is a reference to a folder, natch.)

Honest, that’s what it said, and, that command works every time. Now, who would EVER have tried to use THAT syntax? ; ^ )

Dear Friends,
OK, here’s another little thing I’d like to know. My file gathering and window positioning scripts are working great, thanks to you! But, although I’ like the column view, I’m not at the moment using the left-hand Favorites Pane, and certainly won’t need it for the sub-folders I’m creating.

Is there an AppleScript command to “close” it, that is, push the moveable vertical divider all the way to the left, so that my column view window is showing only the columns? I hope I’m explaining myself clearly. Thanks so much for all your help!

Dear Friends,
Offering another tip most of you probably know. In my old scripts, asking AS for word 1 of Bucky_Edgett returned Bucky. Under OS X it returned Bucky_Edgett. Even after I set the text delimiters to “_”. But asking AS OS X for text item 1 of Bucky_Edgett returned Bucky. Odd, but, as far as I can tell, true. Thanks!