Several little questions

I want to fix several little problems:

⁃	I got a little, silly problem in renaming files: although the privileges are read write and the documents to process are copied into a list, the finder ignores some files during the renaming handler routine. I've to re-run the same script for 4-5 times to rename all the files. Miserable! 

⁃	to count the number of items into a folder, but with shell. The finder is to slow with count for a huge number of documents.

⁃	to preserve a path variable, copying the path into a global variable. In fact, I need the same path for a on open & on run handler, for the next time I launch the script. The path-variable has to change only if I drop another folder onto my applescript-app.

Could you give us the exact error. It’s not clear for us where it goes wrong. Have you tried it with mv shell command (move command) to rename files.

Couldn’t you figure this one out yourself?

Set applescript’s text item delimiters to return and count the text items of the result of ls command or count the text items -2 for ls -al command (including invisible files and ignore . and …)

But I’m more into using the standard addition for this. Don’t use the finder to get folder contents but just use the command list folder to get a list of string with file names in it. Then get the length of this list like:

set fileNameList to list folder (path to desktop folder as string) without invisibles
return length of fileNameList

Application or script data needs to be stored in a file. Next run you can access this file if exists or create a new empty one with default values.

Hi DJ Bazzie Wazzie,

for question 1:

tell application "Finder"
	set x to front window
	set the_c to "New Images"
end tell

set d to 0
repeat with i in x --> i tried with 'count' too
	set d to d + 1
	set the_ex to (name extension of (get info for i as text))
	set nw_nm to (the_c & space & i & "." & the_ex as text)
	--delay 0.2 -->without effect, eventually for update delays
	tell application "Finder" to set name of my_file to nw_nm
end repeat

for answer 2:
thats right. Maybe laziness

for answer 3:
Oh.i thought if applescript is able to remember a string for the next launch, this could apply also for a path. :confused:

The code looks quite strange. The variable d is not used and my_file is not defined

A script preserves the values of properties if it’s saved as application (bundle)
This sample code asks for a folder at the first run and saves the folder after dropping folder(s) onto the droplet

property theFolder : missing value

on run
	if theFolder is missing value then
		set theFolder to (choose folder)
	end if
end run

on open theseItems
	repeat with oneItem in theseItems
		set {folder:Fo, package folder:Pa} to info for oneItem
		if Fo and not Pa then
			set theFolder to contents of oneItem
		end if
	end repeat
end open

on processFolder()
	-- do something
end processFolder

That your is slow has nothing to do with listing a folder. You ask info for for every file and will hold up your script. To get an extension is not so hard at all. I’m wondering even why you are separating the extension from the file, it’s not needed because you have want a prefix instead of a postfix.

A script like this enough (place your own run and open handlers)

set thisFolder to (choose folder) as string
preFixFiles("New Images", thisFolder)

on preFixFiles(preFix, thisFolder)
	set theFiles to list folder thisFolder
	repeat with thisFile in theFiles
		set oldPath to quoted form of POSIX path of (thisFolder & thisFile as string)
		set newPath to quoted form of POSIX path of (thisFolder & preFix & thisFile as string)
		do shell script "mv " & oldPath & space & newPath
	end repeat
end preFixFiles

Hi DJ Bazzie Wazzie:

i agree with you that list folder is a better method to get the contents of a folder. This command isn’t unknown by me but I used it very seldom, mostly passively with some snippets. Maybe because the path gets lost in this kind of list; moreover I considered to little this command. This will change for the next scripts i write

The problem with the extension arises 'cause i use to write it for habit; some of my documents use suffixes which aren’t ‘visible’ as extension; it happens. Sometime i’m also forced to use applescript’s text item delimiters to get the extension’s string. Its a known issue for me.:expressionless:

i know how to build a list. I only talked about it, sorry.

i know the move-rename shell too, but when i can, i try to avoid shells. Sometimes shells are faster and more convenient, if applescript is too clumpsy or slow to do it.
Please see the snippet at the bottom. Now it works how expected.

Hi Stefan:

my_file, in fact, is to substitute with the variable i and d represents an simple counter, specified as zero before the repeat handler. This, because i’m not counting the items in the current handler, otherwise I had to write: repeat with i from 1 to count of items in x.
note: i take the ‘quite strange’ as compliment. :smiley:

ok, its property, not global. And only for applications.

record lists are a really practical way to deal with files. I tried this method time ago, but i often got errors. Maybe because both entries must return as true, as valid entries. Eventually this can be avoid, declaring the variables with default values (e.g: missing value, (),“”)

At last, i’ve modified my snippet into:

tell application "Finder" to set thisFolder to (folder of front window as text)
set theFiles to list folder thisFolder without invisibles
set optionals to "New Images"

set {d, the_ls} to {0, {}}

repeat with i in theFiles
	set d to d + 1
	set the_file to (thisFolder & i as text)
	set the_ex to name extension of (info for (the_file as alias))
	set nw_nm to (optionals & space & d & "." & the_ex as text)
	my rename_file(nw_nm, the_file)
end repeat

on rename_file(nw_nm, the_file)
	tell application "Finder" to set name of (the_file as alias) to nw_nm
end rename_file

Thanks for our input. Inspiration is all.