Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

replace _ with - in file name with applescript

Hello,


This is probably an easy one, but anyone have an applescript that will change "_" to "-" in filename. Want to use it as a folder action. Much appreciated!!

MacBook Pro with Retina display, OS X Yosemite (10.10.5)

Posted on Feb 8, 2016 5:47 PM

Reply
7 replies

Feb 9, 2016 9:19 AM in response to Tony T1

Yes, I know automator can do it, but I need a script. Here's the thing: I need a script to attach to a ChronSync document, so that as ChronoSync syncs folders to it's destination it will also run a script to rename the folders automatically. So I don't have to go back in and do it manually using automator. The thing is, the files that need renaming end up in subfolders, so if I used a folder action to attach to the root level folder, it would have to drill down and apply the filename changed to all files within all subfolders.


Anyone?? Thanks for the help!

Feb 9, 2016 11:30 AM in response to c.g.yogi

c.g.yogi wrote:


Yes, I know automator can do it, but I need a script. Here's the thing: I need a script to attach to a ChronSync document, so that as ChronoSync syncs folders to it's destination it will also run a script to rename the folders automatically. So I don't have to go back in and do it manually using automator. The thing is, the files that need renaming end up in subfolders, so if I used a folder action to attach to the root level folder, it would have to drill down and apply the filename changed to all files within all subfolders.


Anyone?? Thanks for the help!


That Automator is a Folder Action.


Anyway, here's a bash script:


#!/bin/bash


for f in ~/Desktop/untitled\ folder/*

do

path=${f%/*}

basename=${f##*/}

name=${basename%.*}

ext=${f##*.}

name=${name//_/-}

mv "$f" "$path/$name.$ext"

done


To use in Applescript, just use do shell script

You can adapt this for a Folder Action

See also: http://www.macosxautomation.com/applescript/sbrt/sbrt-06.html

A sub-routine for replacing characters in strings:


on replace_chars(this_text, search_string, replacement_string)

set AppleScript's text item delimiters to the search_string

set the item_list to every text item of this_text

set AppleScript's text item delimiters to the replacement_string

set this_text to the item_list as string

set AppleScript's text item delimiters to ""

return this_text

end replace_chars

Feb 9, 2016 6:14 PM in response to leroydouglas

leroydouglas wrote:

I thought file names did not like the -

Always better off with the _

The Finder does not like : (colon).

Shell commands do not like / (forward slash).


If you use the Finder change a name to something like file/type, and you look at it in a Terminal session, you will see file:type


And if you use a shell command to create file:type, it will appear as file/type in the finder.


The original Mac OS used : as a directory separator, and Unix always used / as a directory separator.


The separator is not actually stored on disk (except in symbolic links, but that is more like data). I think the OS X Framework libraries for accessing the file system maintain the original Mac OS : separator notation, and the Unix libraries and system calls maintain the / separator notation.


I do now know what the low level file system API's really accept, and what the application APIs need to convert on the fly before a file name path is passed to the low level file system.

replace _ with - in file name with applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.