I found this code on this forum for removing spaces from files and want to use it in my script. The code I found:
set no_spaces to words 1 through end of (text returned of temp) as string
And the other code I’m using (included is just the snippet where this should be relevant), which is a droplet where I want to rename the files without spaces:
on open these_items
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items)
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if (alias of the item_info is false) and ¬
((the file type of the item_info is in the type_list) or ¬
the name extension of the item_info is in the extension_list) then
process_item(this_item)
end if
end repeat
end open
So I am trying to get that sample code to work properly with this other code, but I keep getting errors when I try to save. I have tried putting my variable in where the “text” is in the sample code and other various things with no luck. It seems like this should be really simple…yet I’ve been scratching my head for quite some time. Can anyone help with this?
Thanks a lot,
j
Nothing wrong with the posted code, post your whole code including handlers, Set the TID before your first script, of course you need a type_list, extension_list.
set AppleScript's text item delimiters to {""}
If you are using AppleScript read and write file, you have to put a try statement and on error close the file. I bet the file is still open. Without the full code its a guess. Restart your machine fixes unclosed files
Thanks for the reply…I think my last post was confusing, as my errors were only coming when I tried to put the new code snippets in my current code. But now that I saw your code snippet, I realized I need to remove the spaces from the filenames in a different part of my script.
So I am calling a function that gets the filename of a file. But I just don’t get how I can remove the spaces from the filename, or better YET would be to replace each space in the filename with an underscore.
“My Movie Is Great.mpg” would become “My_Movie_Is_Great.mpg”. Is this a simple thing to do? Still struggling…
on getFileName(thefile)
set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}
set mytextfilename to last text item of (thefile as text)
set AppleScript's text item delimiters to oldDelims
return mytextfilename
end getFileName
Keep using the delimiters, when you change a list back to a string, it uses the delimiter to divid them
display dialog (getFileName(choose file))
on getFileName(thefile)
set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}
set mytextfilename to last text item of (thefile as text)
set AppleScript's text item delimiters to space
set RemoveSpaces to text items of mytextfilename
set AppleScript's text item delimiters to "_"
set mytextfilename to RemoveSpaces as text
set AppleScript's text item delimiters to oldDelims
return mytextfilename
end getFileName