The final hurdle of my AppleScript!

Hi all,

I solved my previous problem and now only have one last hurdle!!!
To stop returning folders that were not needed I customised it so that only the folders I wanted were returned as a list. This is working quite succesfully.

Now all I want to do is run a loop that systematically goes through all the lists and replaces for instance the [title] with the variable theBookTitle so that the end result is that the folder is now called “Cover-AppleScript” as oppose to “Cover-[title]” that was the original.

So I just need a loop that goes through all the lists searching for certain things in the list names such as [title], [author] etc… just make up any variables and I will replace them.

If anyone could help me with this I would be most appreciative as this is the last thing I need to do!

Thanks,

Dave

Hi Dave,

I’m big on Tanaka’s OSAX which does contain the command xReplace - you can it get for free from osaxen.com - this would be ideal since the text you will be replacing is always static, or will always be something like [title].

Here’s another tip. If you get your lists a different way your script may run better. Try not using the finder so much - I notice faster and more reliable performance. Like:

Instead of saying:

set list2 to contents of folder " [BookTitle]-[Xed]" of folder “000-AAA_[Title23CharsMax]” of destination_folder

try:
set BooksByTitle to “Mac HD:Some Folder:Some Folder:”

tell application “Finder”
set list2 to get the name of every file of folder BooksByTitle
end tell
–that will get you a list of names of files without taxing
the Finder every time you want to get folder contents, and everytime
you want to get the name of a file. Then…

repeat with thisFile in list2
–repeat with every file in your list, define the full path to the
–file so you can work with it
set ThisFilePath to BooksByTitle & thisFile as string
if thisFile contains “[title]” then
–now use the xReplace command
set newName to xReplace thisFile search “[title]” replace theTextBookTitle
–now use the Finder again to rename the file
tell application “Finder”
set the name of file ThisFilePath to newName
end tell
end if
end repeat

Getting the name of every file, (or folder) of a folder and then piecing it
together with the path to the folder it is in just works better and faster. If you don’t have too many files in a folder you may not see a difference. I’ve seen it make a difference after a hundred or so files.

You could also stay away from using Tanaka’s command since you know your file names have “[title]” in them. Just figure out what the name of the file should be and use that instead of replacing text found in the original name

Hope this helps Dave -

Dave,

I just read your other thread here and thought I’d follow up with a minor change.
I thought you were renaming files and it appears you are renaming folders.
So instead of :

tell application “Finder”
set list2 to get the name of every file of folder BooksByTitle
end tell

do this

tell application “Finder”
set list2 to get the name of every folder of folder BooksByTitle
end tell

And try my tip on getting your list of folder names.
This will more than likely fix your “Finder is out of memory” errors.

Best,

Cheers!!! Thanks for the info taken on board!

Will attempt to implement tonight :S

Dave.

p.s - Seeing as you are on a roll :slight_smile: I was just wondering if you know how to take the first 8 characters a user enters into a text box and turn it into a variable?

Thanks again :smiley:

Sure


set CharCountMet to false--this will be set to true when the user enters more than 8 characters

repeat until CharCountMet = true
	tell application "Finder"
		activate
		display dialog "your instructions here" default answer "" with icon note--default answer "" tells the mac to include a text entry box
		set UserInput to the text returned of the result--get the text returned
		set CharCount to the count of characters in UserInput--get the character count
		if CharCount >= 8 then--is it equal to or greater than 8?
			set CharCountMet to true
		else--if not tell them and repeat initial dialog
			display dialog "You need to enter at least 8 characters" buttons {"Try Again"} default button 1 with icon stop
		end if
	end tell
end repeat

set TrimmedUserInput to characters 1 thru 8 of UserInput as string


TrimmedUserInput is now your text entry variable

Good luck,

I’m not sure but I took it that he wanted to create a variable, not place the characters into an existing variable.

Dave, I am terribly sorry. I promise to slow down when I read these from now on.

The first part will get you a guaranteed 8 + character user text entry but to turn it into a variable I only know of 1 way. Using Akua Sweet’s “Universal” Command. I haven’t utilized this very much but basically you would use the same code, or whatever you come up with to get the user input.

set CharCountMet to false--this will be set to true when the user enters more than 8 characters 

repeat until CharCountMet = true 
   tell application "Finder" 
      activate 
      display dialog "your instructions here" default answer "" with icon note--default answer "" tells the mac to include a text entry box 
      set UserInput to the text returned of the result--get the text returned 
      set CharCount to the count of characters in UserInput--get the character count 
      if CharCount >= 8 then--is it equal to or greater than 8? 
         set CharCountMet to true 
      else--if not tell them and repeat initial dialog 
         display dialog "You need to enter at least 8 characters" buttons {"Try Again"} default button 1 with icon stop 
      end if 
   end tell 
end repeat 

set TrimmedUserInput to characters 1 thru 8 of UserInput as string 

Then, add the following lines to set the input to a universal property of the script


universal TrimmedUserInput set with "SomeText"


set ShowVariable to TrimmedUserInput & "=" & (universal TrimmedUserInput as string)


If the user were to enter “MyVariab” in the text entry box then the last line of code, that defines ShowVariable, would produce the string “MyVariab=SomeText”. We defined TrimmedUserInput to be the first 8 characters of the text input. Then, in the first universal command we set the text in TrimmedUserInput to “SomeText”. You could set it to whatever you need. And in the last line we defined ShowVariable to be the contents of TrimmedUserInput and what the contents are universally set with.

I hope this helps and think I may hold off on posting until I can start coming up with useful posts.

Thanks for your patience.

I don’t know about Dave but I hope you don’t stop posting. I still have a lot to learn and I find your posts very useful! I appreciate your willingness to help others. :slight_smile:

Rob - you are too kind. Thanks for the vote of confidence. 8^)

I have only been doing this for a couple of years and try to help when I think I can but sometimes get ahead of myself and fear I may do more harm than good at times. These boards are very beneficial to me and I just want return the favor when I can.

rah rah :smiley: :smiley: :smiley: :smiley:

WE love you Mytzlscript!! Thanks for all your guys help you have solved my problem perfectly! I scripted it in and it works fine! Thanks so much for all your help, Rob + Mytzlscript especially!

Maybe one day I might be able to help some people out :slight_smile:

Thanks again,

Dave