Monday, July 26, 2021

#1 2021-01-06 01:09:45 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

makeFolderFromList (do shell script)

It have been done before and I'm sure this is duplicate.
For any person who think the command-line could be useful and quick.

1. The handler input is AppleScript list of (folder names)
2. It will ask the user to select a parent folder.

Ps. If the folder already exists it will not return a error (try block)

Applescript:

set theFolderList to {"ABC", "1 2 3", "Steve Jobs", "AppleScript Script Library", "A/B/C"}

makeFolderFromList(theFolderList)

on makeFolderFromList(_folderList)
   set theMessage to "Select parent folder"
   set theParentFolder to POSIX path of (choose folder theMessage)
   
   set theListAsString to ""
   set _newFolderList to {}
   
   repeat with i from 1 to count _folderList
       set theItem to "'" & item i of _folderList & "'"
       copy theItem to end of _newFolderList
   end repeat
   
   set ASTID to text item delimiters
   set text item delimiters to space
   set theListAsString to _newFolderList as text
   set text item delimiters to ASTID
   try
       set theResult to do shell script "cd " & theParentFolder & "; " & "mkdir -p " & theListAsString
   end try
end makeFolderFromList

Here is StefanK's approach, thanks StefanK. (string with whitespace need single quote)

Applescript:

makeFolderfromString("/'A B'/C/{'1 2 3',4,5}")

on makeFolderfromString(theString)
   set destinationFolder to POSIX path of (choose folder)
   set theFolderString to theString
   do shell script "mkdir -p " & quoted form of destinationFolder & theFolderString
end makeFolderfromString

Last edited by Fredrik71 (2021-01-07 12:00:35 pm)


if you are the expert, who will you call if its not your imagination.

Offline

 

#2 2021-01-06 11:50:38 am

kerflooey
Member
Registered: 2011-07-07
Posts: 202

Re: makeFolderFromList (do shell script)

Thanks for this, it works great on Mojave.

Offline

 

#3 2021-01-06 12:25:35 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: makeFolderFromList (do shell script)

The nice thing about it...

ex.

Applescript:

set theFolderList to {"A/B/1","A/B/2","A/B/3"}

You will have 3 folders in folder B with name 1, 2, 3.

So it could be very easy to make subfolders.


if you are the expert, who will you call if its not your imagination.

Offline

 

#4 2021-01-06 01:26:42 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11749
Website

Re: makeFolderFromList (do shell script)

Fredrik71 wrote:

The nice thing about it...

ex.

Applescript:

set theFolderList to {"A/B/1","A/B/2","A/B/3"}

You will have 3 folders in folder B with name 1, 2, 3.

So it could be very easy to make subfolders.



Still simpler

Applescript:

set destinationFolder to POSIX path of (choose folder)
set theFolderList to "/A/B/{1,2,3}"
do shell script "mkdir -p " & quoted form of destinationFolder & theFolderList


regards

Stefan

Offline

 

#5 2021-01-06 02:35:25 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: makeFolderFromList (do shell script)

Thanks StefanK, I will call your version makeFolderFromString smile


if you are the expert, who will you call if its not your imagination.

Offline

 

#6 2021-01-07 10:45:24 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: makeFolderFromList (do shell script)

StefanK wrote:

Applescript:

set destinationFolder to POSIX path of (choose folder)
set theFolderList to "/A/B/{1,2,3}"
do shell script "mkdir -p " & quoted form of destinationFolder & theFolderList



StefanK, the limitation in your approach is /A/B/{1,2,3} couldn't include whitespace.
When I made my handler I had that in mind, so you know smile


if you are the expert, who will you call if its not your imagination.

Offline

 

#7 2021-01-07 10:55:03 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11749
Website

Re: makeFolderFromList (do shell script)

Fredrik71 wrote:
StefanK wrote:

Applescript:

set destinationFolder to POSIX path of (choose folder)
set theFolderList to "/A/B/{1,2,3}"
do shell script "mkdir -p " & quoted form of destinationFolder & theFolderList



StefanK, the limitation in your approach is /A/B/{1,2,3} couldn't include whitespace.
When I made my handler I had that in mind, so you know smile



Just put the affected path components in single quotes

Applescript:

set theFolderList to "/'my A'/'your B'/{1,2,3}"


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)