Convert Mac Files With Illegal Pc Characters

Discussion in 'Mac Programming' started by DXBDB, Oct 26, 2009.

  1. macrumors newbie

    Joined:
    Oct 26, 2009
    #1
    Hi

    I have an applescript that I found and was wondering how I can make it work on files and folders within others folders. At the moment it only effects the top level folder and not what is contained within it.

    I'm am moving files onto a windows/unix server and the files will not currently copy because of the illegal characters.

    Would really appreciate the help

    Thanks
    D

    Here is the applescript code:


    Code:
    on run
    	choose file with multiple selections allowed without invisibles
    	open result
    end run
    
    on open droppedItems
    	set illegalCharacters to {",", "/", ":", "<", ">", "^", "*", "[", "]", "=", "+", ";"} -- Whatever you want to eliminate.
    	set replaceWith to "_"
    	set ASTID to AppleScript's text item delimiters
    	launch application "Finder"
    	
    	repeat with thisItem in droppedItems
    		try
    			set newName to name of (info for thisItem without size)
    			
    			repeat with thisChar in illegalCharacters
    				set AppleScript's text item delimiters to {thisChar}
    				set newName to text items of newName
    				set AppleScript's text item delimiters to {replaceWith}
    				set newName to newName as Unicode text
    			end repeat
    			
    			tell application "Finder" to set name of thisItem to newName
    		on error errorMsg number errorNum
    			set AppleScript's text item delimiters to ASTID
    			display dialog "Error " & errorNum & ":" & return & return & errorMsg buttons {"Cancel", "Skip File"} default button 1 with icon caution
    		end try
    	end repeat
    	
    	set AppleScript's text item delimiters to ASTID
    end open
     
  2. thread starter macrumors newbie

    Joined:
    Oct 26, 2009
    #2
    Updated_Convert Mac Files With Illegal Pc Characters

    This applescript now works:


    Code:
    main()
    on main()
    	script o
    		--(A) replace table
    		(* list of search-replace definitions; each entry is either
    {search string, replace string} or {list of search strings, replace string} *)
    		property replaceTable : {{{",", "/", ":", "<", ">", "^", "*", "[", "]", "=", "+", ";"}, "_"}}
    		--property replaceTable : {{"/", "-"}, {"\\", "-"}} -- alternative e.g. (slower)
    		
    		--(B) options
    		property _type : 0 -- type of target item: 0 = any; -1 = directory; 1 = file
    		property _invisible : false -- whether include invisible items or not
    		(* invisible items can be found by this script but not be renamed by Finder *)
    		
    		--(C) files and folders
    		property logpath : "" & (path to "desk") & "renaming log @" & (current date)'s time & ".txt"
    		property srcdir : missing value -- root folder of target tree (given later)
    		
    		--(D) strings, etc
    		property NL : return
    		property str1a : "# Logged at " & (current date)
    		property str1b : NL & NL & "# Search string(s):" & NL
    		property str1c : NL & NL & "# Found item(s):" & NL
    		property str1d : NL & NL & "# Renamed as:" & NL
    		property str2 : NL & NL & "# Operation is canceled by user."
    		property str3 : NL & NL & NL & "### Failed item(s):" & NL
    		property mss1 : "Choose root folder of target tree."
    		property mss2a : "Total of "
    		property mss2b : " items renamed successfully."
    		property errs1 : "Failed to set the name of -" & NL & NL
    		property btt1 : {"Cancel", "Continue"}
    		--(E) working list
    		property aa : {}
    		property xx : {}
    		property ee : {}
    		
    		--(1) select root folder of target tree
    		set srcdir to choose folder with prompt mss1
    		
    		--(2) change item names according to the replaceTable
    		set k to 0 -- total number of renamed items
    		writeData(logpath, str1a, {_append:true})
    		try
    			repeat with r in replaceTable
    				--(2.1) find items
    				set {xx, y} to r's contents
    				if my xx's class is not list then set xx to {my xx}
    				set aa to searchItems(my xx, srcdir, {_type:_type, _invisible:_invisible})
    				
    				--(2.2) log found items
    				writeData(logpath, str1b & list2text(my xx, NL) & str1c & list2text(my aa, NL), {_append:true})
    				
    				--(2.3) change name of found items
    				repeat with a in my aa
    					set a to a's contents
    					tell application "Finder"
    						try
    							set n to a's name
    							repeat with x in my xx
    								set n to my replace(x's contents, y, n)
    							end repeat
    							set a's name to n
    							set k to k + 1
    						on error errs number errn
    							set end of my ee to a
    							activate
    							try
    								reveal a -- reveal the failed item in Finder
    							on error --
    							end try
    							display dialog (errs1 & a & NL & NL & errs & " " & errn) ¬
    								buttons btt1 with icon 2 giving up after 30
    						end try
    					end tell
    				end repeat
    				
    				--(2.4) log renamed items
    				writeData(logpath, str1d & list2text(my aa, NL), {_append:true})
    			end repeat
    		on error number -128
    			--(2.4a) log renamed items, so far
    			writeData(logpath, str1d & list2text(my aa, NL) & str2, {_append:true})
    		end try
    		--(2.5) log failed items, if any
    		if my ee is not {} then writeData(logpath, str3 & list2text(my ee, NL), {_append:true})
    		
    		--(3) decent notice
    		tell application (path to frontmost application as string)
    			display dialog mss2a & k & mss2b with icon 1 giving up after 5
    		end tell
    	end script
    	tell o to run
    end main
    
    on searchItems(xx, r, {_type:_t, _invisible:_i})
    	(*
    list xx : list of search strings, some of which should be matched in item's name
    alias r : alias of root folder of target tree
    integer _t : type of item: 0 for any; -1 for directory; 1 for file
    boolean _i : whether to include invisible items or not
    return list : list of alias of matched items
    *)
    	script o
    		property dd : {r} -- list of alias of target directories where to search
    		property aa : {} -- list of alias of found items whose name contains some of xx
    		property mm : xx as list -- list of search strings
    		property nn : {} -- work list
    		on matched(t)
    			repeat with m in my mm
    				if m is in t then return true
    			end repeat
    			return false
    		end matched
    		
    		repeat until my dd = {}
    			set d to my dd's item 1
    			set my dd to my dd's rest
    			set d_ to "" & d
    			set my nn to list folder d invisibles _i
    			repeat with n in my nn
    				set n to n's contents
    				set a to d_ & n as alias
    				considering case
    					set _dir to "" & a ends with ":"
    				end considering
    				if _dir then -- directory
    					set end of my dd to a
    					if 1 > _t and matched(n) then set end of my aa to a
    				else -- file
    					if _t > -1 and matched(n) then set end of my aa to a
    				end if
    			end repeat
    		end repeat
    		return my aa's contents
    	end script
    	tell o to run
    end searchItems
    
    on replace(x, y, t)
    	(*
    string x, y: search and replace string, respectively
    string t: source string
    return string : replaced string, where x is replaced with y in t
    *)
    	try
    		set AppleScript's text item delimiters to {x}
    		set t to t's text items
    		set AppleScript's text item delimiters to {y}
    		set t to "" & t
    		set AppleScript's text item delimiters to {""}
    	on error errs number errn
    		set AppleScript's text item delimiters to {""}
    		error "replace(): " & errs number errn
    	end try
    	return t
    end replace
    
    on list2text(aa, delim)
    	(*
    list aa : source list
    text delim : text item delimiter in list-text conversion
    *)
    	local t
    	try
    		set AppleScript's text item delimiters to {delim}
    		set t to "" & aa
    		set AppleScript's text item delimiters to {""}
    	on error errs number errn
    		set AppleScript's text item delimiters to {""}
    		error "list2text(): " & errs number errn
    	end try
    	return t
    end list2text
    
    on writeData(fp, x, {_append:_append})
    	(*
    text fp: output file path
    data x: anything to be written to output file
    boolean _append: true to append data, false to replace data
    *)
    	local a
    	try
    		open for access (file fp) with write permission
    		set a to fp as alias
    		if not _append then set eof a to 0
    		write x to a starting at eof
    		close access a
    	on error errs number errn
    		try
    			close access file fp
    		on error --
    		end try
    		error "writeData(): " & errs number errn
    	end try
    end writeData
     

Share This Page