The Mac Observer

 
   
 
Applescript and filenames
Posted: 23 April 2002 07:52 PM [ Ignore ]
Moderator
Total Posts:  3097
Joined  2001-06-11

Can anybody tell me if there’s a way to build an applescript to run under OS9 that will replace certain characters in a filename with a single specified character. It would also need to add a .jpg extension to the end.
I’m afraid I don’t know if applescript is that flexible with respect to filenames.

I’m looking to automate a process to replace all ocurrences of spaces, slashes and periods in filenames with an underscore as well as add a .jpg to the end.

Thanks for any help.
-Dan

 Signature 

"ignorance more frequently begets confidence than does knowledge" - Charles Darwin

What’s the difference between a Mac and a PC? Macs are designed, PCs are assembled.

Profile
 
 
Posted: 21 April 2002 04:35 PM [ Ignore ] [ # 1 ]
Moderator
Total Posts:  3912
Joined  2001-07-13

On 2002-04-21 21:23, Photodan wrote:
Can anybody tell me if there’s a way to build an applescript to run under OS9 that will replace certain characters in a filename with a single specified character. It would also need to add a .jpg extension to the end.
I’m afraid I don’t know if applescript is that flexible with respect to filenames.

I’m looking to automate a process to replace all ocurrences of spaces, slashes and periods in filenames with an underscore as well as add a .jpg to the end.

Thanks for any help.
-Dan

Dan -

If no one steps up to the plate, PM me, and I’ll see what I can do for you by way of a Perl script, if that’s a valid option.

 

 

 Signature 

Work is the curse of the drinking classes.
- Oscar Wilde

Profile
 
 
Posted: 22 April 2002 06:34 AM [ Ignore ] [ # 2 ]
stars_5
Total Posts:  1162
Joined  2001-11-02

If you do I’d like to see that perl script, it sounds like about 3 lines of perl code, and I’d love to learn a little more there. 

perhaps this should be a contest, who can do it in the least number of lines (or characters, this is perl after all icon_smile.gif ) just one long regexp…

 Signature 

All statements are to be taken with a grain of salt

Profile
 
 
Posted: 23 April 2002 01:09 PM [ Ignore ] [ # 3 ]
Administrator
Avatar
Total Posts:  882
Joined  2001-06-17

This sounds like a job for an AppleScript Guru (Can I have some hero music please??  icon_smile.gif )

Okay, this is what I thought up if a few minutes.  I hope it will work.  It’s a droplet.  Drag the files you need changed onto it and it will do its thing.  I have it set to finding the ” ” character and replacing it with the “-” character but you can have it do whatever you want.  Then it tags a .jpg onto the file.  If the new file name is too long, the script does not change the name of the file.  It assumes that all the files are good in length.  I hope this helps you a bit.  If you need any changes, let me know.  There’s the code.  Paste it in your script editor and then save as an application (make sure to check Never Show Startup Screen):

on run
    display dialog 
"To use this script drop a file onto its icon." buttons ["OK"default button 1
end run
(*What do do if the user double-clicks me?*)
on open TheFiles
    repeat with file1 in TheFiles
        tell application 
"Finder"
            
set the_name to name of file1 as string
        end tell
        
(*Get name of file*)
        
set num to number of characters in the_name
        set new_name to 
""
        
repeat with i from 1 to num
            
if character i of the_name is " " then
                
(*This is the character you want to fine*)
                
set new_name to new_name "-" as string
                
(*This is the character you want to replace it with*)
            else
                
set new_name to new_name character i of the_name as string
            end 
if
        
end repeat
        
(*Find replacefind replacefind replace*)
        
set new_name to new_name ".jpg" as string
        
(*Add .jpg*)
        
tell application "Finder"
            
try
                
set name of file1 to new_name
                
(*Change the name*)
            
on error
            end 
try
            (*
Woops the file name was too long.  Oh wellMove on.*)
        
end tell
    end repeat
end open 

 

Profile
 
 
Posted: 23 April 2002 06:42 PM [ Ignore ] [ # 4 ]
stars_5
Total Posts:  1162
Joined  2001-11-02

c’mon tbone, are you going to take that sitting down?  I want to see a five line perl script that does all that in half the time and 1/10 the code readability icon_biggrin.gif

 Signature 

All statements are to be taken with a grain of salt

Profile
 
 
Posted: 23 April 2002 07:44 PM [ Ignore ] [ # 5 ]
Moderator
Total Posts:  3097
Joined  2001-06-11

Very cool Burnum!

I made a slight modification to make the script search out 3 different characters (space, period and slash) to replace.

if character i of the_name is " " or character i of the_name is "/" or character i of the_name is "." then 

Thanks a lot!

-Dan

 

 Signature 

"ignorance more frequently begets confidence than does knowledge" - Charles Darwin

What’s the difference between a Mac and a PC? Macs are designed, PCs are assembled.

Profile
 
 
Posted: 23 April 2002 07:52 PM [ Ignore ] [ # 6 ]
Moderator
Total Posts:  3097
Joined  2001-06-11

Don’t beat up on tbone too badly. We actually tried working on it for a little bit the other night. He almost got it to work. It would run but it didn’t change anything in the filename.
Here’s the code that we have so far:

$directory "Macintosh HD:Convertfilenames";

chdir $directory or die "can't change to directory $directory : $!";

opendir S_DIR$directory or die "Could not open directory $directory : $!
"
;

foreach (<
S_DIR>)
{
   $new 
$_;
   
$new =~ s/ /_/g;
   
$new =~ s/./_/g;
   
$new =~ s///_/g;
   
rename $_$new ".jpg" or print "Could not rename file $_ to $new .jpg:
  $!"
;
}

closedir S_DIR

Anybody perl-people see the problem?

(BTW, Thanks a lot for trying tbone!)
-Dan

_________________
“Ok, you people! Sit tight, hold the fort and keep the home fires burning. And if we’re not back by dawn… call the president.” - Kurt Russell, from Big Trouble in Little China

<font size=-1>[ This Message was edited by: Photodan on 2002-04-24 00:53 ]</font>

 Signature 

"ignorance more frequently begets confidence than does knowledge" - Charles Darwin

What’s the difference between a Mac and a PC? Macs are designed, PCs are assembled.

Profile
 
 
   
 
 

Apple Stock Quote (AAPL)

Loading...

Hot Topics

TMO Express

Join the TMO Express Daily Newsletter to get the latest Mac headlines in your e-mail every weekday. Find out more!

Top Deals From DealBrothers.com

Recent Features

Support The Mac Observer

We noticed you may be running AdBlock on your computer. It takes real money to run this site and to deliver the news, tips, and opinions you love to read.

If you wish to block the ads that pay for the creation of our content, we ask that you instead support TMO Directly, either with a $5 monthly recurring contribution, or a one-time donation of any amount of your choice. Thanks!

Subscribe with Paypal Donate with Paypal