• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: forbidden characters in applescript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: forbidden characters in applescript


  • Subject: Re: forbidden characters in applescript
  • From: Philip Regan <email@hidden>
  • Date: Mon, 10 Jan 2005 13:41:56 -0800

Here's some code I wrote to help me with the exact same problem. We
 have a specific file naming convention at my company for all of our
 book files (putting Mac files onto Windows servers), and it's
 impossible to go through thousands of files by hand each time a book
 comes in.


 It's slow as can be, and not all the features work, but it does get
 the job done. Edit as you see fit...


 [code]



property searchStrings : {"&", "+", "#", "!", "@", "$", "%", "^", "*", "(", ")", "-", "=", "[", "]", "<", ">", "?", " ", ";", "/", "\\", "\"", ",", "{", "}", "'", "."}
property replacementStrings : {"and", "p", "s", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "."}

global search_string
global replacement_string
global periodFlag

set periodFlag to false

tell application "Finder"
	set source_folder to choose folder with prompt "FindReplaceIllegalCharacters: Please select directory."

	set startTime to (current date)

	repeat with x from 1 to (get count of items in searchStrings)
		if x = 28 then
			set search_string to (item x of searchStrings)
			my countPeriods(source_folder)
		else
			set search_string to (item x of searchStrings)
			set replacement_string to (item x of replacementStrings)
			my createList(source_folder)
		end if
	end repeat

	set endTime to (current date)
	set runTime to (endTime - startTime)
	set runTimeText to ("RUNTIME: " & (runTime as text) & " seconds.")
	set changedFileLog to (((path to desktop folder) as text) & "ChangedFileLog.txt")
	my write_to_file(runTimeText, changedFileLog, true)

	if periodFlag is true then
		display dialog "Some files had extra periods in them. Please check the log and fix the filenames."
	end if
end tell

on countPeriods(item_list) --currently lists all files with a period. Need to coerce filename into a list, and/or view each *character* in turn and see if it's a period.
	set periodCount to 0
	set the the_items to list folder item_list without invisibles
	set item_list to item_list as string
	repeat with i from 1 to number of items in the the_items
		set the_item to item i of the the_items
		set the_item to (item_list & the_item) as alias
		set this_info to info for the_item
		set item_name to (name of this_info) as string
		if item_name contains search_string then
			set text item delimiters to search_string
			set periodCount to (count text items of item_name) - 1
			if periodCount > 1 then
				set periodFlag to true
				set periodFilename to ((the_item as string) & return)
				set periodFilenameLog to (((path to desktop folder) as text) & "FilenamesWithExtraPeriods.txt")
				my write_to_file(periodFilename, periodFilenameLog, true)
			end if
		end if
		if folder of this_info is true then
			my countPeriods(the_item)
		end if
	end repeat
end countPeriods

on createList(item_list)
	set the the_items to list folder item_list without invisibles
	set item_list to item_list as string
	repeat with i from 1 to number of items in the the_items
		set the_item to item i of the the_items
		set the_item to (item_list & the_item) as alias
		set this_info to info for the_item
		set the item_name to the name of this_info
		if item_name contains search_string then
			set changedFile to ((the_item as string) & return)
			set changedFileLog to (((path to desktop folder) as text) & "ChangedFileLog.txt")
			my write_to_file(changedFile, changedFileLog, true)
			my replaceString(item_name, the_item, search_string, replacement_string)
		end if
		if folder of this_info is true then
			my createList(the_item)
		end if
	end repeat
end createList

on replaceString(current_name, the_item, search_string, replacement_string)
	set AppleScript's text item delimiters to search_string
	set the text_item_list to every text item of the current_name
	set AppleScript's text item delimiters to replacement_string
	set the new_item_name to the text_item_list as string
	set AppleScript's text item delimiters to ""
	my set_item_name(the_item, new_item_name)
end replaceString

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file
 [/code]

 HTH,

 --

 Philip Regan


 On 10 Jan 2005, at 08:58, Jan-Bultereys wrote:


 hi,


 I am trying to isolate the problem with forbidden characters,

 but I am not able to declare this in applescript


 I know you can 'overwrite' a forbidden character with the && but I am
 not able to

 compile the variable "forbidden"


 The idea is to declare "FileName" (check all the forbidden characters)
 and when it DOES contain those forbidden characters,

 replace it with an _ (underscore)


 any idea's are much appreciated.



 --set forbidden to "&&< > : " / \ | *?&&"

 display dialog forbidden

 repeat with temp_file in file_list

 	set FileName to name of temp_file

 	if FileName contains "/" or "(" or ")" or "&" then

 		display dialog "You are using forbidden character"

 	end if

 end repeat

  _______________________________________________

 Do not post admin requests to the list. They will be ignored.

 Applescript-users mailing list      (email@hidden)

 Help/Unsubscribe/Update your Subscription:



 This email sent to email@hidden




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >forbidden characters in applescript (From: "Jan-Bultereys" <email@hidden>)

  • Prev by Date: Re: Hidden Folders
  • Next by Date: Re: Hidden Folders
  • Previous by thread: Re: forbidden characters in applescript
  • Next by thread: RE: forbidden characters in applescript
  • Index(es):
    • Date
    • Thread