Newby help - Folder create, rename by a special algorithm

I’ll try to make a script, which creates a new folder automatically with a name and a number at the end e.g. Titel Wort 2

This should be managed within another script which’s result is a name containing parts of the new-to-create folder name. The new folder should be named with a +1 number at the end compared to an existing folder within the main-folder “Ordner1” or another sub-folder “Bearbeitet” within the main-folder.
A further problem is, that I get a text from a still existing and running script like: “hier ist Titel Wort mit dem Zusatz…” or in another case: “das wäre Titel mit Namen 1D sowieso”, the new-folder-text is always part of the scripts result text.

sounds complicated and perhaps is so, too :frowning:
I made a screenshot, hoping it makes that easier

http://www.deschler-web.de/Bilder/Buch/Bild.jpg

so the next folder should be “Titel Wort 2” or “Titel 1D 64” or “Titel Text 7” depends on what I get as result from the other script.
The maximum of different folder-names is about 60, the sub-folder contains roundabout 2000 existing folders, this example is only with 3 different folder-names and five folders in the sub-folder and 8 folders in the main-folder

greetings to all, who can help me a little

Model: iMac G5
AppleScript: 1.10.7
Browser: Firefox 3.5.3
Operating System: Mac OS X (10.4)

I would say, try to explain it again…
step by step…:confused:
like

Step 1
Ordner1/Titel 1D 63
Ordner1/Bearbeitet/Titel 1D 61

Step 2
Ordner1/Titel 1D 64
Ordner1/Bearbeitet/Titel 1D 62

just to see what the result should look like

:slight_smile:
you’re right, step by step would be recommended :rolleyes:

First: there already exists this folder “Order1” with a lot of folders in it as shown in the picture.
My existing script now does this:

set ordnername to “Titel” & items 2 thru 5 of text items 1 thru -1 of chaptername as text

try
if not (exists (folder ordnername of folder theLocation)) then
make new folder at folder theLocation with properties {name:ordnername}
select folder ordnername of folder theLocation
else
set ordnername2 to ordnername & " 1"
make new folder at folder theLocation with properties {name:ordnername2}
beep 2
set Fehlernachricht to ("This Folder already exists:
" & ordnername)
display dialog Fehlernachricht giving up after dialog_timeout
select folder ordnername2 of folder theLocation
end if

end try

tell application “TextEdit”
activate
set newDoc to make new document
tell newDoc
set its text to textcontent
end tell
end tell
set theDocumentPath to (Speicherort) & (documentName as text) & “.rtfd” as text

this is to open roundabout 2000 TextEdit files automatically, read a line and extract there a chaptername for the folder where it saves a new modified TextEdit File a few lines below

That already works fine
but my Problem is, that the chaptername has roundabout 60 different names and only a part of it (a special text and a number) is relevant for the foldername of the folder to be created and this text varies in lenght an position of its interesting content so I used
set ordnername to “Titel” & items 2 thru 5 of text items 1 thru -1 of chaptername as text
to get rid of the not important parts of the origin text but it’s still too long.
This is one problem I can’t solve
as one can see the example shows that there are only short passages relevant as e.g. only a number “17” or only a word “text” or both of it “1D”

The second problem is:
creating a folder which has a name with an incremental number at the end of its name, which depends on the highest number of a still existing folder in the mainfolder “Order1” or its subfolder “Bearbeitet”

so the steps would be:
set finallyName to ??? of ordnername to get a short name like in the picture
set finallyNamewithNumber to finallyName & currentHighestNumberOfExistingFolderWithThatSameName +1 of finallyName

greetings tommoni

Hm, still a bit confusing…
so first at all, numbering folders:

checkname_folder("Neuer Ordner", (choose folder), false)
on checkname_folder(n, D, looped)
	set counter to 1
	set nn to n & " " & counter
	tell application "Finder"
		set thefiles to name of every folder of (D as alias)
	end tell
	if thefiles contains nn then
		
		repeat until thefiles does not contain nn
			set counter to counter + 1
			set nn to n & " " & counter
		end repeat
		return nn
	else
		return n
	end if
end checkname_folder

http://picfront.de/d/EKDEufF7ZZ5/Bildschirmfoto2009-10-23um02.17.09.png
on this folder it returns Neuer Ordner 9
http://picfront.de/d/taCBL5Pq43/Bildschirmfoto2009-10-23um02.17.49.png
on this folder it returns Neuer Ordner 2
so you will get number folder names…

second:
a faster way to get a rtf’s text could be

rtf2txt(POSIX path of (choose file of type {"rtf", "rtfd", "txt"}))
on rtf2txt(rtfpath)
	return do shell script "textutil -stdout -convert txt " & quoted form of rtfpath
end rtf2txt

third:

Perhaps examples on these chapter names would be helpful, still have no idea what this special text and a number could look like…

Hello and thanks for your help!

I’ll try the your first two solutions this weekend and hope to get it all to run :rolleyes:

The question about the titles of those folders is answered easily - I hope

I made a screenshot, to be downloaded here
http://www.deschler-web.de/Bilder/Buch/Bild.jpg

My existing Script already makes folders e.g. with name: “this Titel is about the discussion of Wort” and I have to maually rename it to “Titel Wort” and have to look in the sub-folder “Bearbeitet” whether there is a still existing folder (here in my example there isn’t) then to look in the main folder (there is one, named “Titel Wort 1” so the next folder should be “Titel Wort 2”

The same rules are for the rest of the 2000 automacically Script-generated folders with 60 different folder-names like “Titel 1D 64” or “Titel Text 7” depends on what I get as result from the existing script.
This example is only with 3 different folder-names and five folders in the sub-folder and 8 folders in the main-folder. In reality I have now roundabout 2200 folders with 60 different names. 2000 of them are in the sub-folder “Bearbeitet” and of no interest exept the name of the last folder to get the result of the highest number.
In my example this is e.g. “Titel 1D 61” when I receive a new generated folder which I have to rename from “Topic 1D with an Epilog of Titel” to “Titel 1D #?” and then renumber it with the latest (highest) following number of #64 because Titel 1D 63 and Titel 1D 64 still exist in the main folder.

hope this helps a little to un-confuse this situation :confused:

Hi,

I tried to get the Script to run - and it works :slight_smile:
But there are two problems:

  1. the Script doesn’t recognize the circumstance, that old and obsolete folders could have been deleted e.g. folders below “Titel Text 3” in folder “Bearbeitet” and so it names the current folder “Titel Text 1” although there is a folder “Titel Text 4” in Bearbeitet and “Titel Text 5” and “Titel Text 6” in the main folder. The correct name of the new folder would be “Titel Text 7”
    if I added a few lines to the Script:
set n to "Titel Text" --to test it only with this folder-name
set DActive to "Ordner1" --the active folder containing folders to be worked with
set DEnd to "Ordner1:Bearbeitet" --the non-active folder containing folders which are completed and no longer in use
set looped to false

checkname_folder(n, DActive, DEnd, false)
on checkname_folder(n, DActive, DEnd, looped)
	set counter to 1
	set nn to n & " " & counter
	
	tell application "Finder"
		set thefilesActive to name of every folder of (DActive as alias)
		set thefilesEnd to name of every folder of (DEnd as alias)
		set thefiles to thefilesEnd & thefilesActive
		end tell

		if thefiles contains nn then
			display dialog "Topic already exists; new folder is created"
			repeat until thefiles does not contain nn
				set counter to counter + 1
				set nn to n & " " & counter
			end repeat

			set nname to nn
		else
			set nname to n
		end if
		
end checkname_folder

tell application "Finder"
	make new folder at folder D with properties {name:nname}
end tell
  1. and the second problem is that now the Script doesn’t return values for nn or n and finally the nname isn’t defined; but I’ll need this variable to make a new folder with that name

I now can break with this mental line: giving up after 3 hours :mad: needing further assistance

I tried to paste the rtftext into the existing Script:


tell application "TextEdit"
    activate
    set newDoc to make new document
    tell newDoc
        set its text to textcontent
    end tell
end tell
set theDocumentPath to (Speicherort) & (documentName as text) & ".rtfd" as text
	
	
	set theDocumentPath to (Speicherort) & (documentName as text) & ".rtfd" as text
	if item (theDocumentPath) exists then
		display alert "Datei existiert! bereits"
	else
		
		try
			tell application "TextEdit" to save document 1 in theDocumentPath
			
		on error
			display alert "Cannot save " & theDocumentPath as text
		end try
	end if
	tell application "TextEdit" to close document 1
	
end tell

but how can I integrate this into my script? I mean, I get the text in the variable textcontent and have to save it as rtf or textclip in a folder, but if I understood that correctly those following lines convert an existing file into rtf… or am I wrong with comprehension?

rtf2txt(POSIX path of (choose file of type {"rtf", "rtfd", "txt"}))
on rtf2txt(rtfpath)
   return do shell script "textutil -stdout -convert txt " & quoted form of rtfpath
end rtf2txt