Thursday, July 9, 2020

#1 2020-06-20 10:46:56 am

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Increment folder number if exists

Hi

I am trying to name my projects a certain way i.e.

PROJ01
PROJ02

SAMP01
SAMP02

If no folder exists with my chosen 4 characters then it will start with 01, it will then increment there after


I have the following script that takes my input text, changes it to uppercase and limits the characters to 4.

I can't work out how to check if certain folders exists and then increment the name if they do.

Here's what I have so far.

Applescript:


-- Get Current Folder
tell application "Finder"
   if exists Finder window 1 then
       set CurrentDir to target of Finder window 1 as alias
   else
       set CurrentDir to desktop as alias
   end if
end tell
set CurrentPath to POSIX path of (CurrentDir)

-- Create a Project Name
set ProjectNamePrompt to text returned of (display dialog "Please enter your Projects Name Business Name:" default answer "" buttons {"Cancel", "Create"} default button "Create")

-- Change text to Upper Case
set UpperProjectNamePrompt to do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').upper().encode('utf8')\" " & quoted form of ProjectNamePrompt

-- Limit text to 4 characters
set NewName to text 1 thru 4 of UpperProjectNamePrompt -- as chosen returns a list by default it must be flattened

Offline

 

#2 2020-06-20 11:35:23 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

Here is a script doing the job (as I understood it).
For tests I disabled some instructions and used a predefined value for NewName.

Applescript:

property forTests : true
-- true --> use a predefined NewName
-- false --> activate the instructions designed to create NewName

-- Get Current Folder
tell application "Finder"
   if exists Finder window 1 then
       set CurrentDir to target of Finder window 1 as string
   else
       set CurrentDir to desktop as string
   end if
end tell
--set CurrentPath to POSIX path of (CurrentDir)
if not forTests then
   -- Create a Project Name
   set ProjectNamePrompt to text returned of (display dialog "Please enter your Projects Name Business Name:" default answer "" buttons {"Cancel", "Create"} default button "Create")
   
   -- Change text to Upper Case
   set UpperProjectNamePrompt to do shell script "python -c \"import sys; print unicode(sys.argv[1], 'utf8').upper().encode('utf8')\" " & quoted form of ProjectNamePrompt
   
   -- Limit text to 4 characters
   set NewName to text 1 thru 4 of UpperProjectNamePrompt -- as chosen returns a list by default it must be flattened
else
   set NewName to "SAMP"
end if

tell application "System Events"
   set maybe to name of every folder of folder CurrentDir whose name starts with NewName
   set fullName to NewName & text -2 thru -1 of ((101 + (count maybe)) as string)
   make new folder at end of folder CurrentDir with properties {name:fullName}
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 20 juin 2020 19:34:48

Offline

 

#3 2020-06-20 01:45:00 pm

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

@Peavine

Thank you for posting, I got the following error with your code.

"error "Finder got an error: AppleEvent handler failed." number -10000"

@Yvan Koenig

Thank you for posting, You code seems to work great but I have an issue.

You changed the following section from an alias to a string

Applescript:


tell application "Finder"
   if exists Finder window 1 then
       set CurrentDir to target of Finder window 1 as string
   else
       set CurrentDir to desktop as string
   end if
end tell
set CurrentPath to POSIX path of (CurrentDir)

I need to get the name of a couple of folders and I use a line like the following to get it

Applescript:


tell application "Finder" to set ProjectsName to name of CurrentDir

If I use the following line, it gives me the full url and not the folder name

Applescript:


tell application "Finder" to set ProjectsName to CurrentDir

Offline

 

#4 2020-06-20 01:59:42 pm

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Just playing around and worked out this lines gives me the same result.

Applescript:


tell application "Finder" to set ProjectsName to name of folder (CurrentDir)

Offline

 

#5 2020-06-20 02:36:20 pm

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

@peavine

Apologies, it works perfect, I must have copied it wrong.

One slight problem is that it should start with i.e. SAMP01 and not just SAMP but thanks

Last edited by Danjuan (2020-06-20 02:36:33 pm)

Offline

 

#6 2020-06-21 03:43:45 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

Danjuan wrote:

@Yvan Koenig

Thank you for posting, You code seems to work great but I have an issue.

You changed the following section from an alias to a string

Applescript:


tell application "Finder"
   if exists Finder window 1 then
       set CurrentDir to target of Finder window 1 as string
   else
       set CurrentDir to desktop as string
   end if
end tell
set CurrentPath to POSIX path of (CurrentDir)

I need to get the name of a couple of folders and I use a line like the following to get it

Applescript:


tell application "Finder" to set ProjectsName to name of CurrentDir

If I use the following line, it gives me the full url and not the folder name

Applescript:


tell application "Finder" to set ProjectsName to CurrentDir




I coerced as string because I always does my best to not use the Finder which I hate.
And, given recent messages I assumed that System Events would not accept to work with aliases when running under Catalina.

For what we are doing, System Events is faster than Finder. If you really need to work with the Finder, you may pick instructions in the code below.

Applescript:



tell application "Finder"
   if exists Finder window 1 then
       set CurrentDir to target of Finder window 1 as alias
   else
       set CurrentDir to desktop as alias
   end if
end tell
set CurrentPath to POSIX path of CurrentDir --> "/Users/**********/Desktop/"

set NewName to "SAMP"
tell application "Finder" to set ProjectsName to name of CurrentDir --> Desktop
tell application "Finder" to set ProjectsName to CurrentDir --> alias "SSD 1000:Users:**********:Desktop:"
log (current date) (*date dimanche 21 juin 2020 à 11:09:12*)
tell application "System Events"
   set maybe to name of every folder of CurrentDir whose name starts with NewName
   set fullName to NewName & text -2 thru -1 of ((101 + (count maybe)) as string)
   make new folder at end of CurrentDir with properties {name:fullName} --> folder "SSD 1000:Users:**********:Desktop:SAMP01:"
end tell
log (current date) --> date dimanche 21 juin 2020 à 11:09:12-- required less than 1 second
tell application "Finder"
   set maybe to name of every folder of folder (CurrentDir as string) whose name starts with NewName
   set fullName to NewName & text -2 thru -1 of ((101 + (count maybe)) as string)
   make new folder at CurrentDir with properties {name:fullName} --> folder "SAMP02" of folder "Desktop" of folder "**********" of folder "Users" of startup disk
end tell
log (current date) --> date "dimanche 21 juin 2020 à 11:09:26" -- yes, required 14 seconds
set CurrentDir to CurrentDir as string
tell application "Finder" to set ProjectsName to name of folder CurrentDir --> Desktop
tell application "Finder" to set ProjectsName to CurrentDir --> "SSD 1000:Users:**********:Desktop:"
set CurrentPath to POSIX path of CurrentDir --> "/Users/**********/Desktop/"


log (current date) (*date dimanche 21 juin 2020 à 11:09:26*)
tell application "System Events"
   set maybe to name of every folder of folder CurrentDir whose name starts with NewName
   set fullName to NewName & text -2 thru -1 of ((101 + (count maybe)) as string)
   make new folder at end of folder CurrentDir with properties {name:fullName} --> folder "SSD 1000:Users:**********:Desktop:SAMP03:"
end tell
log (current date) (*date dimanche 21 juin 2020 à 11:09:26*)
tell application "Finder"
   set maybe to name of every folder of folder CurrentDir whose name starts with NewName
   set fullName to NewName & text -2 thru -1 of ((101 + (count maybe)) as string)
   make new folder at (folder CurrentDir) with properties {name:fullName} --> folder "SAMP04" of folder "Desktop" of folder "**********" of folder "Users" of startup disk
end tell
log (current date) --> date dimanche 21 juin 2020 à 11:09:41 -- yes, required 15 seconds

And of course I disabled the instruction building a POSIX Path because it wasn't used.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 21 juin 2020 11:20:29

Offline

 

#7 2020-06-21 04:16:59 am

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Hi

I don't need to work with Finder, it's just the code I've got to know.

Simple things like...

Applescript:


-- Get the current grand-parent folder Path
tell application "Finder" to set ClientsFolderPath to get (parent of parent of (CurrentDir)) as text

I don't know how to do this with system preferences.

Back to the main task, after playing about with the codes I have found an issue that I didn;t think of before.

If the project name was i.e. s. amPLe

I would get the following result

S. A

which is 4 characters. How would I ignore spaces, special characters such as .,';"@ etc. I would also like to ignore 'The'.

Is this possible?

Offline

 

#8 2020-06-21 06:20:37 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

You may use :

Applescript:

use AppleScript version "2.4" -- required
use framework "Foundation" -- to convert
use scripting additions -- into UPPERCASE

set CurrentDir to (path to documents folder as string) & "Important:mon bureau:des scripts:Scripts ASObjC:about PDF:collect-highlight in PDF:"
tell application "System Events" to set ClientsFolderPath to path of container of container of folder CurrentDir
--> "SSD 1000:Users:**********:Documents:Important:mon bureau:des scripts:Scripts ASObjC:"

-- I use this scheme because I assume that we don't want to remove “the” if it's in a word like Athen
set someText to "the theorem of : ƒPythagoras # / €"
-- the colon and the slash are removed in some languages but I guess that they aren't in some others
set allWords to words of someText --> {"the", "theorem", "of", "ƒPythagoras", "€"}
set someWords to {}
repeat with aWord in allWords
   set aWord to aWord as string
   if not aWord is "The" then set end of someWords to aWord
end repeat
someWords --> {"theorem", "of", "ƒPythagoras", "€"}
set someText to my recolle(someWords, space) --> "theorem of ƒPythagoras €"
-- remove space chars an some chars which aren't welcome in a file name. I added other possibly unwanted chars
set cleanedText to my supprime(someText, {space, "/", ":", "ƒ", "€"}) --> "theoremofPythagoras"
set shortText to text 1 thru 4 of cleanedText -- > "theo"
set upperText to ((current application's NSString's stringWithString:shortText)'s uppercaseString()) as string --> "THEO"

#=====

on recolle(l, d)
   local oTIDs, t
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end recolle

#=====
(*
removes every occurences of d in text t
*)

on supprime(t, d)
   local oTIDs, l
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set l to text items of t
   set AppleScript's text item delimiters to ""
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end supprime

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 21 juin 2020 14:13:23

Last edited by Yvan Koenig (2020-06-21 07:27:06 am)

Offline

 

#9 2020-06-21 01:55:40 pm

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Thank you both for your help.

@Peavine

I am playing about with your script at the moment because I understand it more.

How would I go about putting your code within an if statement i.e.

Applescript:


tell application "Finder"
   if ProjectsFolder = "Projects" then
       --Prompt to Choose a Client Name
       set ProjectNamePrompt to text returned of (display dialog "Please enter your project's business name:" default answer "" buttons {"Cancel", "Create"} default button "Create")

set BaseName to cleanText(ProjectNamePrompt)

if BaseName contains "the" then
display dialog "The folder name will contain the letters " & quote & "the" & quote buttons {"Cancel", "Continue"} default button 2 with icon caution
end if

set i to 1

repeat 99 times
set NewName to BaseName & (text -2 thru -1 of ("0" & i))
try
alias ((CurrentDir as text) & NewName)
set i to i + 1
on error
tell application "Finder" to make new folder at CurrentDir with properties {name:NewName}
exit repeat
end try
end repeat

on cleanText(theText)

set lowerCase to "abcdefghijklmnopqrstuvwxyz"
set upperCase to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set theNumbers to "1234567890"

set cleanedText to {}
set characterCount to 0

repeat with aCharacter in theText
set aCharacter to contents of aCharacter
if aCharacter is in theNumbers then
set end of cleanedText to aCharacter
set characterCount to characterCount + 1
else if aCharacter is in lowerCase then
set end of cleanedText to (item (offset of aCharacter in lowerCase) of upperCase)
set characterCount to characterCount + 1
end if
if characterCount = 4 then return (cleanedText as text)
end repeat

display dialog "The project name does not contain four usable characters." buttons {"OK"} cancel button 1 default button 1 with icon stop

end cleanText
       
       
   else
       display dialog "Open your Clients 'Projects' folder first" buttons {"OK"} default button "OK" with icon caution --or use icon stop
   end if
end tell

I get the following error when I try to save the script

Expected “else”, etc. but found “on”.

Offline

 

#10 2020-06-21 02:22:09 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

You may try with :

Applescript:


tell application "Finder"
   if ProjectsFolder = "Projects" then
       --Prompt to Choose a Client Name
       set ProjectNamePrompt to text returned of (display dialog "Please enter your project's business name:" default answer "" buttons {"Cancel", "Create"} default button "Create")
       
       set BaseName to cleanText(ProjectNamePrompt)
       
       if BaseName contains "the" then
           display dialog "The folder name will contain the letters " & quote & "the" & quote buttons {"Cancel", "Continue"} default button 2 with icon caution
       end if
       
       set i to 1
       
       repeat 99 times
           set NewName to BaseName & (text -2 thru -1 of ("0" & i))
           try
               alias ((CurrentDir as text) & NewName)
               set i to i + 1
           on error
               tell application "Finder" to make new folder at CurrentDir with properties {name:NewName}
               exit repeat
           end try
       end repeat
       
   else
       display dialog "Open your Clients 'Projects' folder first" buttons {"OK"} default button "OK" with icon caution --or use icon stop
   end if
end tell

on cleanText(theText)
   
   set lowerCase to "abcdefghijklmnopqrstuvwxyz"
   set upperCase to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   set theNumbers to "1234567890"
   
   set cleanedText to {}
   set characterCount to 0
   
   repeat with aCharacter in theText
       set aCharacter to contents of aCharacter
       if aCharacter is in theNumbers then
           set end of cleanedText to aCharacter
           set characterCount to characterCount + 1
       else if aCharacter is in lowerCase then
           set end of cleanedText to (item (offset of aCharacter in lowerCase) of upperCase)
           set characterCount to characterCount + 1
       end if
       if characterCount = 4 then return (cleanedText as text)
   end repeat
   
   display dialog "The project name does not contain four usable characters." buttons {"OK"} cancel button 1 default button 1 with icon stop
   
end cleanText

But the script will not convert accented lowercase chars if there are some in the entered string.
More, there is no reason to behave this way if there is the word "the" in the entered string.
Taking care of this case is really simple. It's done in my proposal.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 21 juin 2020 22:21:32

Offline

 

#11 2020-06-21 03:35:49 pm

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Hi

Unfortunately you can't add 'on cleanText(theText)' underneath 'end tell'

it throws up the following error:

Finder got an error: Can’t continue cleanText.

I don't want to get into what code is better than the other. I'm sure both codes work well enough for what I want but the simple answer for me is, I understand Peavines code.

Offline

 

#12 2020-06-21 08:42:20 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6397

Re: Increment folder number if exists

peavine wrote:

English does not use accented lowercase characters.


But plenty of English speakers have files that use them, and work with companies whose names use them.

if the OP wanted the script to work in languages other than English he should have asked



Sorry, I think you have that the wrong way around. If anything, where a solution works only in English, it's up to the person posting the solution to note the limitation.

MacScripter is not just about solving a particular user's problem, but also providing a searchable reference site for other users. They may require some English to read the site, but they should not be required to run English-language systems to use the code.

Language-agnostic code should be welcomed, and preferred where practical.

That said, I'm guessing that the OP does want the final four-letter string limited to the characters A-Z0-9. And I'm not sure either solution posted meets that requirement -- one potentially capitalizes accented characters, resulting in characters outside the A-Z range, and the other simply drops them, which is also potentially unsatisfactory.

Here's an alternative approach that deletes any initial "The ", then uses a transform to convert everything to ASCII equivalents (so é becomes e, for example), then to capitalize and strip anything left not in A-Z0-9:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later only
use framework "Foundation"
use scripting additions

cleanTheText("The Éclairs R Us")
on cleanTheText(theText)
   set aString to current application's NSString's stringWithString:theText
   if aString's hasPrefix:"The " then set aString to aString's substringFromIndex:4
   set aString to aString's stringByApplyingTransform:"Any-Latin; ASCII; Upper; [^A-Z0-9] Remove" |reverse|:false
   try -- errors if string too short
       return text 1 thru 4 of (aString as text)
   end try
end cleanTheText

Last edited by Shane Stanley (2020-06-21 08:43:26 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#13 2020-06-22 02:32:30 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

Thank you Shane.

I just wonder : what need to drop the É (as well as œ Œ æ Æ) which are accepted in file names ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 22 juin 2020 10:32:09

Offline

 

#14 2020-06-22 03:56:32 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6397

Re: Increment folder number if exists

Yvan Koenig wrote:

I just wonder : what need to drop the É (as well as œ Œ æ Æ) which are accepted in file names ?



I'm guessing it's not a matter of what is acceptable in file names, but what is preferred for project codes. I can understand having a preference for codes restricted to a basic alphabet, even if what they represent is a bit more complex.

But I'm just surmising in terms of the OP.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#15 2020-06-22 04:03:50 am

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Thank you all for your help so far.

The codes are purely to help me search. I thought if I just keep it to 4 digits, capitals and no special characters then it would make it easier on the eye.

I've found a slight issue that I forgot to mention before.

So far my folders output is for instance. ORIO01, ORIO02, ORIO03

This works but I want to add the actual company name before it so it would be like

Orion Building Suppliers (ORIO01)
Orion Building Suppliers (ORIO02)
Orion Building Suppliers (ORIO03)

I managed to get this output by using the following code:

Applescript:


repeat 99 times
   set NewName to new & (text -2 thru -1 of ("0" & i))
   try
       alias ((CurrentDir as text) & ClientName & " (" & NewName & ")")
       set i to i + 1
   on error
       tell application "Finder" to make new folder at CurrentDir with properties {name:ClientName & " (" & NewName & ")"}
       exit repeat
   end try
end repeat

BUT....

If I had another client called 'Orion Painting Co' it would also output (ORIO01) so I would have a duplicate of the 4 digit code (ORIO01) like so..

Orion Building Suppliers (ORIO01)
Orion Painting Co (ORIO01)

Where as it needs to be
Orion Building Suppliers (ORIO01)
Orion Painting Co (ORIO02)
Orion Bubble Gums (ORIO03)
Capcom (CAPC01)
Capcone (CAPC02)
etc

Offline

 

#16 2020-06-22 07:06:05 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

Danjuan wrote:

Hi

Unfortunately you can't add 'on cleanText(theText)' underneath 'end tell'

it throws up the following error:

Finder got an error: Can’t continue cleanText.

I don't want to get into what code is better than the other. I'm sure both codes work well enough for what I want but the simple answer for me is, I understand Peavines code.




You must explain better what you really want.
In what you posted, you started with a test upon the content of a variable : ProjectsFolder which was not defined.
What I posted assumed that there is code before defining this variable.

Below is a script which makes some assumptions to fill this gap.

Applescript:

-- three instructions required by the cleanTheText handler
use AppleScript version "2.5" -- macOS 10.11 or later only
use framework "Foundation"
use scripting additions

property forTests : true
-- if you set it to true, the script build random client names
-- if you set it to false, you are asked to enter client names

property originalOrder : true
-- if you set it to true, the names will resemble to "Capcom (CAPC02)"
-- if you set it to false the names will resemble to "(ORIO15) Orion Bubble Gums"

tell application "Finder"
   if exists Finder window 1 then
       set CurrentDir to target of Finder window 1 as string
   else
       set CurrentDir to (desktop as string) & "Projects:"
   end if
   set ProjectsFolder to name of folder CurrentDir
end tell -- no longer speak to Finder

if ProjectsFolder = "Projects" then
   if forTests then
       -- For tests, two instructions building random client names
       set someNames to {"Orion Building Suppliers", "Orion Painting Co", "Orion Bubble Gums", "Capcom", "Capcone"}
       set ClientName to some item of someNames --pick a random name
   else -- standard code
       -- Prompt to Choose a Client Name. Edited so that it will not create a folder if you press Cancel.
       set ClientName to text returned of (display dialog "Please enter your project's business name:" default answer "" buttons {"Cancel", "Create"} default button "Create" cancel button "Cancel")
   end if --forTests
   set BaseName to "(" & my cleanTheText(ClientName)
   
   tell application "System Events"
       set maybe to name of every folder of folder CurrentDir whose name contains BaseName
       -- here we have (count maybe) folders whose name contains BaseName
       -- create a new one incrementing the embedded number
       if originalOrder then
           set fullName to ClientName & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")"
       else
           set fullName to BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ") " & ClientName
       end if -- originalOrder
       make new folder at end of folder CurrentDir with properties {name:fullName} --> folder "SSD 1000:Users:**********:Desktop:Projects:Capcom (CAPC02):"
   end tell -- "System Events"
else
   display dialog "Open your Clients 'Projects' folder first" buttons {"OK"} default button "OK" with icon stop --or use icon caution
end if

on cleanTheText(theText)
   -- handler designed by Shane Stanley
   set aString to current application's NSString's stringWithString:theText
   if aString's hasPrefix:"The " then set aString to aString's substringFromIndex:4
   set aString to aString's stringByApplyingTransform:"Any-Latin; ASCII; Upper; [^A-Z0-9] Remove" |reverse|:false
   try -- errors if string too short
       return text 1 thru 4 of (aString as text)
   end try
end cleanTheText

After running it 8 times, the folder sorted by name contain:
Capcom (CAPC02)
Capcom (CAPC05)
Capcone (CAPC01)
Capcone (CAPC03)
Capcone (CAPC04)
Orion Building Suppliers (ORIO01)
Orion Building Suppliers (ORIO02)
Orion Painting Co (ORIO03)

From my point of view it would be better to reverse the order of components in the name.
After running it 22 times the folder contain:
(CAPC01) Capcom
(CAPC02) Capcom
(CAPC03) Capcom
(CAPC04) Capcom
(CAPC05) Capcom
(CAPC06) Capcom
(CAPC07) Capcone
(ORIO01) Orion Painting Co
(ORIO02) Orion Painting Co
(ORIO03) Orion Painting Co
(ORIO04) Orion Painting Co
(ORIO05) Orion Bubble Gums
(ORIO06) Orion Building Suppliers
(ORIO07) Orion Bubble Gums
(ORIO08) Orion Painting Co
(ORIO09) Orion Building Suppliers
(ORIO10) Orion Painting Co
(ORIO11) Orion Bubble Gums
(ORIO12) Orion Painting Co
(ORIO13) Orion Bubble Gums
(ORIO14) Orion Building Suppliers
(ORIO15) Orion Bubble Gums

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 22 juin 2020 15:01:40

Last edited by Yvan Koenig (2020-06-22 07:35:53 am)

Offline

 

#17 2020-06-22 10:33:52 am

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Thank you so much.

That has got me where I need to be or on the right track to finish my project.

I am trying to streamline my process in dealing with my clients. Rather than manually creating a folder each time, I want to be able to press a button in the Finder Toolbar which creates the folder for me. When doing this it will also duplicate the contents of a template folder so every clients folder will be formatted the exact same way with all the same folders.

As well as doing that, I want to format the text in a certain way so the names all appear the same, each client will then be associated a code so it's easy to search. That code will be then used in the projects folder to keep track of versions i.e. ORIO01-0001, ORIO01-0002 etc.

I agree that the code should go at the start to be more pleasing to the eye so I will decide soon whether to go with that instead.

The reason why details were left out was because I started out trying to do my projects folder but when doing so, soon realised that I was missing features in my clients folders which i need to populate my projects folders.

I now believe I can get both scripts working exactly how I wanted thank to everyones help.

The next phase will be......

Can I create folder remotely from a website or mainly from online software called Airtable.com or Coda.io. Both have API's and I would like to sync folders to that data. I'm more inclined to go with Coda.io because of it's features.

Basically I would like to go on my phone, login to my site or the app, input my clients details and all the folders will be installed on my mac

Offline

 

#18 2020-06-22 10:53:16 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

Danjuan wrote:


The next phase will be......

Can I create folder remotely from a website or mainly from online software called Airtable.com or Coda.io. Both have API's and I would like to sync folders to that data. I'm more inclined to go with Coda.io because of it's features.

Basically I would like to go on my phone, login to my site or the app, input my clients details and all the folders will be installed on my mac



I never used networks and refuse to own a smartphone so I guess that what you ask is outside my area of expertise.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 22 juin 2020 18:53:09

Offline

 

#19 2020-06-22 02:23:39 pm

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Sorry one last thing.

If for instance I had a client called: 'The Bluechip company'

At the moment the folder will be created as '(BLUE01) The Bluechip Company'.

Is it possible that IF the name contains 'The' move it to the back i.e. '(BLUE01) Bluechip Company, The'.

Other than that, your script is perfect.

Offline

 

#20 2020-06-23 02:20:07 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5239

Re: Increment folder number if exists

Shane Stanley wrote:

Here's an alternative approach that deletes any initial "The ", then uses a transform to convert everything to ASCII equivalents (so é becomes e, for example), then to capitalize and strip anything left not in A-Z0-9:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later only
use framework "Foundation"
use scripting additions

cleanTheText("The Éclairs R Us")
on cleanTheText(theText)
   set aString to current application's NSString's stringWithString:theText
   if aString's hasPrefix:"The " then set aString to aString's substringFromIndex:4
   set aString to aString's stringByApplyingTransform:"Any-Latin; ASCII; Upper; [^A-Z0-9] Remove" |reverse|:false
   try -- errors if string too short
       return text 1 thru 4 of (aString as text)
   end try
end cleanTheText


I realise Shane's intention was simply to illustrate the transform, but this version of the handler's possibly a bit more robust in practice:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later only
use framework "Foundation"
use scripting additions

cleanTheText("The Éclairs R Us")
on cleanTheText(theText)
   if ((theText begins with "The ") and ((count theText) > 4)) then set theText to text 5 thru -1 of theText
   set aString to current application's NSString's stringWithString:theText
   set theText to (aString's stringByApplyingTransform:"Any-Latin; ASCII; Upper; [^A-Z0-9] Remove" |reverse|:false) as text
   if ((count theText) < 4) then return false -- Or some other error token.
   return text 1 thru 4 of theText
end cleanTheText


NG

Offline

 

#21 2020-06-23 07:42:44 am

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Thank you

I'll give that a go.

In the meantime, due to the change in code part of my old code that worked no longer fits.

I am trying to check whether the customer exists and display an error if it does. Then copy a folder from a template location and rename it, here's my the code:

Applescript:


tell application "Finder"
   if exists folder ClientDestinationFolderString then
       display dialog "That client already exists!"
   else
       do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED
   end if
end tell

Here's the new code

Applescript:


tell application "System Events"
   set maybe to name of every folder of folder CurrentDir whose name contains BaseName
   set fullName to combined & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")"
   make new folder at end of folder CurrentDir with properties {name:fullName}
   move ClientTemplatePath to ClientsDestinationPath
   
end tell

Offline

 

#22 2020-06-23 08:39:56 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

Danjuan wrote:

Thank you

I'll give that a go.

In the meantime, due to the change in code part of my old code that worked no longer fits.

I am trying to check whether the customer exists and display an error if it does. Then copy a folder from a template location and rename it, here's my the code:

Applescript:


tell application "Finder"
   if exists folder ClientDestinationFolderString then
       display dialog "That client already exists!"
   else
       do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED
   end if
end tell


would be better to code:

Applescript:

tell me to do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED

because you are triggering a function belonging to a scripting addition in a tell application block.

Danjuan wrote:

Here's the new code

Applescript:


tell application "System Events"
   set maybe to name of every folder of folder CurrentDir whose name contains BaseName
   set fullName to combined & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")"
   make new folder at end of folder CurrentDir with properties {name:fullName}
   move ClientTemplatePath to ClientsDestinationPath
end tell


As I am not a sooth sayer, I can't guess what are the variables
combined
CurrentDir
ClientTemplatePath
ClientsDestinationPath
supposed to contain.
I just have a vague idea about CurrentDir.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 23 juin 2020 16:39:32

Last edited by Yvan Koenig (2020-06-23 08:46:06 am)

Offline

 

#23 2020-06-23 10:28:40 am

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Apologies, I didn't think.

Here is the full (WORKING) script.

Issues I have found are:

• I don't want two companies of the same name
Currently if you add a company and add the company again, it will simply increase the number of the code. I need this to check for the business name exists first. This need to be checked against two locations which are: ClientsArchivePath & ClientsDestinationPath

• I need to copy a the contents of a template folder
When the client folder is created, I need to copy the contents over from: ClientTemplatePath
Or simply copy the folder to the new location and then rename it.

I was doing this using the following code:

The old code I used to determine if a folder existed and copy the template folder

Applescript:


tell application "Finder"
   if exists folder ClientDestinationFolderString then
       display dialog "That client already exists!"
   else
       do shell script "cp -R " & ClientTemplatepath & space & ClientDestinationFolder -- EDITED
   end if
end tell

But I would like to do it without using Finder if possible as mentioned on here due to the speed.

Here is the full code

Applescript:


use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

--> Needed for Git
set UserEmail to quoted form of "name@email.com"
set UserName to quoted form of "Full Name"

--> Physical URL Path of the Client Template Folder
set ClientTemplatePath to "Volumes:GoogleDrive:My Drive:Work:Resources:Templates:OS Folders:Client Template:"

--> Physical URL Path of the Project Template Folder
set ProjectTemplatePath to "Volumes:GoogleDrive:My Drive:Work:Resources:Templates:OS Folders:Project Template:"

--> Physical URL Path of the Client Archived Folder
set ClientsArchivePath to "Google Drive:My Drive:Work:Clients (Archived):"

--> Physical URL Path of the Local Client Folder
set ClientsDestinationPath to "Macintosh HD:Users:daniel:Documents:Clients:"

--> Get Current Folder Path (CurrentPath) and Name (ClientsFolder)
tell application "Finder"
   if exists Finder window 1 then
       set CurrentDir to target of Finder window 1 as string
   else
       set CurrentDir to (desktop as string)
   end if
   --> Get Current Folder Name
   set ClientsFolder to name of folder CurrentDir
   --> Get Current Folder path
   set CurrentPath to CurrentDir
end tell

--> Prompt to Choose a Client Name.
set ClientNamePrompt to text returned of (display dialog "Please enter your Clients Business Name:" default answer "" buttons {"Cancel", "Create"} default button "Create" cancel button "Cancel")

set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s capitalizedString()) as string

--> Word Exraction
set allWords to words of CapitalizedText
set someWords to {}
set otherWords to {}
repeat with aWord in allWords
   set aWord to aWord as string
   if not aWord is "The" then set end of someWords to aWord
   if aWord is "The" then set end of otherWords to aWord
end repeat
someWords --> {"theorem", "of", "ƒPythagoras", "€"}
set someText to my recolle(someWords, space)
set otherText to my recolle(otherWords, space)
set other to someText
set thetext to otherText
set combined to other & ", " & thetext

--> Start of the cleaning process and creation of the client folder
set BaseName to "(" & my cleanTheText(combined)
tell application "System Events"
   set maybe to name of every folder of folder CurrentDir whose name contains BaseName
   set fullName to combined & space & BaseName & text -2 thru -1 of ((101 + (count maybe)) as string) & ")"
   if CurrentDir contains combined then
       display dialog "That client already exists!"
   else
       display dialog "That client dpoes not exists!"
       make new folder at end of folder CurrentDir with properties {name:fullName}
   end if
end tell

on recolle(l, d)
   local oTIDs, t
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end recolle

--> Removes the special characters from ClientNamePrompt
on cleanTheText(thetext)
   -- handler designed by Shane Stanley
   set aString to current application's NSString's stringWithString:thetext
   if aString's hasPrefix:"The " then set aString to aString's substringFromIndex:4
   set aString to aString's stringByApplyingTransform:"Any-Latin; ASCII; Upper; [^A-Z0-9] Remove" |reverse|:false
   try -- errors if string too short
       return text 1 thru 4 of (aString as text)
   end try
end cleanTheText

Offline

 

#24 2020-06-23 11:33:39 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Increment folder number if exists

I apologizes but I didn't became a sooth sayer since my late message.

What is the variable ClientDestinationFolderString supposed to contain ?
Is it the path to the folder created by the instruction:

Applescript:

make new folder at end of folder CurrentDir with properties {name:fullName}

I wish to add some comments about the block of instructions:

Applescript:

set ClientNamePrompt to text returned of (display dialog "Please enter your Clients Business Name:" default answer "" buttons {"Cancel", "Create"} default button "Create" cancel button "Cancel") --> "the theorem of : ƒPythagoras # / the €"

set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s capitalizedString()) as string--> "THE THEOREM OF : ƒPYTHAGORAS # / THE €"

--> Word Exraction
set allWords to words of CapitalizedText --> {"THE", "THEOREM", "OF", "ƒPYTHAGORAS", "THE", "€"}
set someWords to {}
set otherWords to {}
repeat with aWord in allWords
   set aWord to aWord as string
   if not aWord is "The" then set end of someWords to aWord
   if aWord is "The" then set end of otherWords to aWord
end repeat
someWords --> {"THEOREM", "OF", "ƒPYTHAGORAS", "€"}
otherWords --> {"THE", "THE"}
set someText to my recolle(someWords, space) --> "THEOREM OF ƒPYTHAGORAS €"
set otherText to my recolle(otherWords, space) --> "THE THE"
set other to someText --> "THEOREM OF ƒPYTHAGORAS €"
set theText to otherText --> "THE THE"
set combined to other & ", " & thetext --> "THEOREM OF ƒPYTHAGORAS €, THE THE"

At this time, if  ClientNamePrompt contain several times the word "The",
combined will contain several times the word "THE".
Is it really useful ?
If it's not the block may be replaced by:

Applescript:

set ClientNamePrompt to text returned of (display dialog "Please enter your Clients Business Name:" default answer "" buttons {"Cancel", "Create"} default button "Create" cancel button "Cancel") --> "the theorem of : ƒPythagoras # / the €"

set CapitalizedText to ((current application's NSString's stringWithString:ClientNamePrompt)'s capitalizedString()) as string --> "THE THEOREM OF : ƒPYTHAGORAS # / THE €"

--> Word Exraction
set allWords to words of CapitalizedText --> {"THE", "THEOREM", "OF", "ƒPYTHAGORAS", "THE", "€"}
set someWords to {}
set theText to ""
repeat with aWord in allWords
   set aWord to aWord as string
   if aWord is "THE" then
       set theText to aWord
   else
       set end of someWords to aWord
   end if
end repeat
someWords --> {"THEOREM", "OF", "ƒPYTHAGORAS", "€"}
set combined to my recolle(someWords, space) --> "THEOREM OF ƒPYTHAGORAS €"
if theText ≠ "" then set combined to combined & ", " & theText --> "THEOREM OF ƒPYTHAGORAS €, THE"

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 23 juin 2020 19:33:33

Offline

 

#25 2020-06-24 04:29:38 am

Danjuan
Member
Registered: 2020-04-02
Posts: 48

Re: Increment folder number if exists

Thank you.

I'm almost ready to post the full code but I need to add another folder location to the following line so it checks two folders.

Applescript:


set maybe to name of every folder of folder ClientsDestinationPath whose name contains BaseName

The following code works:

Applescript:


tell application "System Events"
set maybe to name of every folder of folder ClientsDestinationPath whose name contains BaseName
end tell

And so does this:

Applescript:


tell application "System Events"
set maybe to name of every folder of folder ClientsArchivedPath whose name contains BaseName
end tell

I have tried the following but it won't work

Applescript:


tell application "System Events"
set maybe to name of every folder of folder ClientsDestinationPath & ClientsArchivedPath whose name contains BaseName
end tell

I have also tried:

Applescript:


tell application "System Events"
set maybe to name of every folder of folder ClientsDestinationPath & name of every folder of folder ClientsArchivedPath whose name contains BaseName
end tell

Last edited by Danjuan (2020-06-24 04:35:45 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)