Hi there! I’m Jennifer and I’m new. I’ve been a programmer and a mac user both for about four years, but I’m finally just starting to try Applescript, which is awesome, by the way, and I wish I’d started sooner.
I have a real back-to-basics sort of question. I have searched and googled for about 8 work-hours and I’m ready to appeal to real people for help. Even though I have a work-around, I’d really prefer to do it the “right” way.
I’m working on a project where my user will create videos on her MacBook using iMovie, and then when she’s happy with her movie, she’ll drag and drop it into a destination folder which is attached to the Applescript which will launch all sorts of magical actions and publish her video on her website.
The first thing I want it to do is rename the file to a safe filename that’s not going to bork the rest of the processes. Specifically, to replace the spaces and semicolons with hyphens (iMovie, WHY do you put semicolons in the filename?!). I’m doing the regex work in an external perl script, which is going well, but when it comes to doing the actual file rename, it fails.
on adding folder items to this_folder after receiving these_items
repeat with aFile in these_items
set cleanFileName to do shell script "perl /Users/*****/newsroom/cleanFileName.pl \"" & aFile & "\";"
do shell script "echo \"" & aFile & "\" > /Users/*****/Desktop/file;"
do shell script "echo \"" & cleanFileName & "\" >> /Users/*****/Desktop/file;"
tell application "Finder"
try
set name of aFile to cleanFileName
on error
display dialog "couldn't rename file"
end try
end tell
end repeat
end adding folder items to
I’ve tried about 1000 permutations of the line “set name of aFile to cleanFileName” and it always comes back with the error. I’ve tried using move instead of set name, I’ve tried using aliases instead, etc. It just seems like such a basic thing, and I’m feeling pretty foolish that I’m already stumped this early in the process.
The output in the Desktop/file file is:
Macintosh:Users:ToPublish:test; file.txt
Macintosh:Users:ToPublish:test–file.txt
So I know the perl script is doing its thing and the full paths are being stored in the variables. I can’t imagine what I’m missing. Unless it’s the semicolons and spaces that are messing it up in the first place, in which case I’m in a Catch-22.
Anyway, like I said, I do have a workaround, which is to use mv on do shell script.
on adding folder items to this_folder after receiving these_items
repeat with aFile in these_items
set cleanFileName to do shell script "perl /Users/*****/newsroom/cleanFileName.pl \"" & aFile & "\";"
set dirtyPosix to POSIX path of aFile
set cleanPosix to POSIX path of cleanFileName
do shell script "echo \"" & dirtyPosix & "\" > /Users/*****/Desktop/file;"
do shell script "echo \"" & cleanPosix & "\" >> /Users/*****/Desktop/file;"
do shell script "mv \"" & dirtyPosix & "\" \"" & cleanPosix & "\";"
end repeat
end adding folder items to
That does work, so I’m not overly worried, but if I’m really going to get into Applescripting like I’d like to, I figure I should probably start doing things the right way from the start.
Here’s where it gets really weird, though. The Desktop/file output of that second script is:
/Users//Desktop/ToPublish/test–file.txt
/Users//Desktop/ToPublish/test–file.txt
but if I comment out the mv line, it is:
/Users//Desktop/ToPublish/test; file.txt
/Users//Desktop/ToPublish/test–file.txt
It’s like it’s doing the mv before the do shell script echoes, or some weird aliasing or something. I would think a scripted language would do the lines in order, but anyway, that’s just quirky, it’s not really my question.
Sorry this post has gotten way longer than I intended. I’m really excited about this project and getting into Applescript. I’ll keep updating on my progress if anyone’s interested. Oh, and just to make this post just a little bit longer, here’s the perl code for the cleanFileName.pl script:
#!/usr/bin/perl
@data = @ARGV;
$data = join(“-”,@data);
$data =~ s/;/-/g;
$data =~ s/\s/-/g;
print “$data\n”;
Model: MacBook
AppleScript: 1.10.7
Browser: Safari 525.22
Operating System: Mac OS X (10.4)