Re: Questions from Newbie
Re: Questions from Newbie
- Subject: Re: Questions from Newbie
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 28 Feb 2001 09:20:57 -0800
On 2/28/01 3:27 AM, "Max Salven" <email@hidden> wrote:
>
What I'm trying to achieve is the following:
>
>
I want to create an applescript which I can drop a folder containing lots of
>
files and folders with nested files.
>
>
Then, for each file, the applescript should:
>
1. Get the Finder comments of the file (ie what you see in Get Info) into a
>
variable, item_comments.
>
2. Delete all but the last 36 characters of item_comments.
>
3. Delete the last 4 characters of item_comments.
>
4. Rename the file to item_comments.
>
>
The reason why I need to do this, is because I receive a lot of files via
>
FTP, but the filenames are always truncated so that it's only the first 32
>
characters of the file name.
<snip>
>
Also, I'd appreciate tips on how to parse the "item_comments" properly. All
>
I want it to do is take the right 36 characters, and then the left 32
>
characters. In VBScript (ugh) it would be something as simple as this:
>
item_comments = left(right(item_comments,36),32)
>
But how do I do it in apple script?
Well, that part is even easier in AppleScript:
set item_comments to text -36 thru -5 of item_comments
-1 is the last item (character) of text or of a list, -2 is second-to-last,
etc. Although fle names actually can't be more than 31 characters long (not
32), so better make that
set item_comments to text -35 thru -5 of item_comments
What you're asking for is pretty easy, except for the "folder containing
lots of files and folders with nested files" bit. There's a Finder property,
"entire contents", which is meant to handle just such circumstances, but it
is notorious for being unreliable (it gives you an incomplete list without
alerting you under conditions which I don't think anyone knows for absolute
certain, making it worse). So we like to avoid that one. There are good
solutions using 3rd-party osaxen, but here's one just using basic
AppleScript and the Standard Additions that come with your Mac. i can't test
it because i don't keep info comments (they're all blank); you may possibly
hit some problems with recursive handlers and memory if there are too many
folders within folders. Save this as an Application (droplet).
on open theSelection -- any group of files or folders
repeat with theItem in theSelection
my FileOrFolder(theItem) -- do the handler to find out
end repeat
beep
display dialog "All done!"
end open
set theItem to alias "PB HM Hard Disk:Desktop Folder:Desktop Crap:"
FileOrFolder(theItem)
on FileOrFolder(theItem)
set itemInfo to (info for theItem)
if folder of itemInfo then -- if it's a folder
tell application "Finder" to set folderContents to every item of
theItem
repeat with i from 1 to (count folderContents)
set innerItem to item i of folderContents
my FileOrFolder(innerItem) -- recursive
end repeat
else if visible of itemInfo then -- if it's a file (omit invisibles)
my Rename(theItem)
end if
end FileOrFolder
on Rename(theItem)
tell application "Finder"
set itemComments to comment of theItem
set commentLength to (count itemComments)
if commentLength > 34 then
set itemComments to text -35 thru -5 of itemComments
else if commentLength > 4 then
set itemComments to text commentLength thru -5 of itemComments
end if
if commentLength > 4 then set name of theItem to itemComments
end tell
end Rename
--
Paul Berkowitz