{"id":351,"date":"2011-06-23T14:37:15","date_gmt":"2011-06-23T21:37:15","guid":{"rendered":"http:\/\/10.0.1.201\/?p=351"},"modified":"2011-06-23T14:37:15","modified_gmt":"2011-06-23T21:37:15","slug":"replace-space-with-underscore","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2011\/06\/23\/replace-space-with-underscore\/","title":{"rendered":"Replace space with underscore"},"content":{"rendered":"<p>An AppleScript to replace spaces (&#8216; &#8216;) with underscore (&#8216;_&#8217;) in names of files selected in the Finder. It does not do folders, but should be easy enough to modify so it can.<br \/>\n<!--more--><br \/>\nIt does display a prompt with the file names as a list, so processing a mountain of files may not be ideal since AppleScript dialogs have the unwelcome habit of pushing the top (the only place you can grab an AppleScript dialog) and bottom (where the buttons are) past the edges of the screen when they contain enough text. Although &#8216;OK&#8217; and &#8216;Cancel&#8217; can still be activated by <em><b>return<\/b><\/em> and <em><b>Command .<\/b><\/em> respectively.<\/p>\n<p><ins datetime=\"2019-12-22T15:48:35+00:00\">[UPDATE] Removed some redundant text in repeat routines, and switched to &#8220;my text item delimiters&#8221;.<\/ins><\/p>\n<p><code>(*<br \/>\nhttp:\/\/strawhousepig.net\/<br \/>\nPortions of this script from:<\/p>\n<p>Replace Text In Item Names<br \/>\nCopyright \u00ef\u00bf\u00bd 2001 Apple Computer, Inc.<br \/>\n*)<\/p>\n<p>tell application \"Finder\" to set the_items to selection<br \/>\nset name_list to \"\"<br \/>\nrepeat with i in the the_items<br \/>\n\tset this_item to i as alias<br \/>\n\tset this_info to info for this_item<br \/>\n\trepeat 1 times<br \/>\n\t\tif folder of this_info is true then<br \/>\n\t\t\texit repeat<br \/>\n\t\t\t--set this_name to \"(Folder) \" & this_name<br \/>\n\t\tend if<br \/>\n\t\tset this_name to name of this_info & return<br \/>\n\t\tset name_list to name_list & this_name<br \/>\n\tend repeat<br \/>\nend repeat<br \/>\nif name_list is not \"\" then<br \/>\n\tdisplay dialog \"Replace spaces with underscores in the following files?\" & return & return & name_list<br \/>\nelse<br \/>\n\tdisplay dialog \"Please select files first.\"<br \/>\n\treturn<br \/>\nend if<\/p>\n<p>repeat with i in the the_items<br \/>\n\tset this_item to i as alias<br \/>\n\tset this_info to info for this_item<br \/>\n\trepeat 1 times<br \/>\n\t\tif folder of this_info is true then<br \/>\n\t\t\texit repeat<br \/>\n\t\tend if<br \/>\n\t\tset the current_name to the name of this_info<br \/>\n\t\t-- replace target string using delimiters<br \/>\n\t\tset my text item delimiters to \" \"<br \/>\n\t\tset the text_item_list to every text item of the current_name<br \/>\n\t\tset my text item delimiters to \"_\"<br \/>\n\t\tset the new_item_name to the text_item_list as string<br \/>\n\t\tmy set_item_name(this_item, new_item_name)<br \/>\n\tend repeat<br \/>\nend repeat<\/p>\n<p>on set_item_name(this_item, new_item_name)<br \/>\n\ttell application \"Finder\"<br \/>\n\t\t--activate<br \/>\n\t\tset the parent_container_path to (the container of this_item) as text<br \/>\n\t\tif not (exists item (the parent_container_path & new_item_name)) then<br \/>\n\t\t\ttry<br \/>\n\t\t\t\tset the name of this_item to new_item_name<br \/>\n\t\t\ton error the error_message number the error_number<br \/>\n\t\t\t\tif the error_number is -59 then<br \/>\n\t\t\t\t\tset the error_message to \"This name contains improper characters, such as a colon (:).\"<br \/>\n\t\t\t\telse --the suggested name is too long<br \/>\n\t\t\t\t\tset the error_message to error_message -- \"The name is more than 31 characters long.\"<br \/>\n\t\t\t\tend if<br \/>\n\t\t\t\t--beep<br \/>\n\t\t\t\ttell me to display dialog the error_message default answer new_item_name buttons {\"Cancel\", \"Skip\", \"OK\"} default button 3<br \/>\n\t\t\t\tcopy the result as list to {new_item_name, button_pressed}<br \/>\n\t\t\t\tif the button_pressed is \"Skip\" then return 0<br \/>\n\t\t\t\tmy set_item_name(this_item, new_item_name)<br \/>\n\t\t\tend try<br \/>\n\t\telse --the name already exists<br \/>\n\t\t\t--beep<br \/>\n\t\t\ttell me to display dialog \"This name is already taken, please rename.\" default answer new_item_name buttons {\"Cancel\", \"Skip\", \"OK\"} default button 3<br \/>\n\t\t\tcopy the result as list to {new_item_name, button_pressed}<br \/>\n\t\t\tif the button_pressed is \"Skip\" then return 0<br \/>\n\t\t\tmy set_item_name(this_item, new_item_name)<br \/>\n\t\tend if<br \/>\n\tend tell<br \/>\nend set_item_name<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An AppleScript to replace spaces (&#8216; &#8216;) with underscore (&#8216;_&#8217;) in names of files selected in the Finder. It does not do folders, but should be easy enough to modify so it can.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,23,28],"class_list":["post-351","post","type-post","status-publish","format-standard","hentry","category-code","tag-applescript-2","tag-macintosh","tag-os-x"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=351"}],"version-history":[{"count":0,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}