Friday, June 18, 2021

#1 2021-06-09 01:14:58 am

p_T
Member
Registered: 2021-06-09
Posts: 2

mkdir bash script embedded in Applescript issues

I am trying to create an AppleScript that will create a folder structure in the current active Finder window using mkdir. The mkdir command works as expected when run from the terminal but I am not sure why its not working in the AppleScript.

Applescript:

tell application "Finder"
set the theFilePath to (folder of the front window as alias)
set theFilePath2 to POSIX path of theFilePath
do shell script "#!/bin/bash mkdir -p " & quoted form of theFilePath2 "FTC0000\ Job/{_Brief,Artwork,Dispatch/{Low\ Res,Hi\ Res/},History,Links,Supplied}"
end tell

I have previously made the folder structures in AppleScript but the larger more complex ones (not the example above) take a long to build when running and I think the above solution should be more efficient if made to work.

The AppleScripts are shared with other users in our studio and run from the Script Editor menu bar drop down.

Any help appreciated.

Offline

 

#2 2021-06-09 02:34:36 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 45

Re: mkdir bash script embedded in Applescript issues

Applescript:

tell application "Finder"
   set fp to (target of the front window as alias)
   set fp2 to POSIX path of fp
end tell
   
do shell script "mkdir -p " & quoted form of fp2 & "FTC0000\\ Job/{_Brief,Artwork,Dispatch/{Low\\ Res,Hi\\ Res/},History,Links,Supplied}"

A few minor thingsā€¦

The '&' between 'fp2' and the folder arguments is missing.

The backslashes in your FTC string need to be escaped with their own '\'.

Move the 'do shell' outside the Finder tell block. It does execute but it also generates a needless error. Also, finder windows have a target property which I used.

I'm not too familiar with the subtleties of the shell, but I think that the '#!' isn't required here (or maybe it even breaks things). This is more akin to running a one-liner in the terminal than a .sh file. Also, 'do shell' doesn't need to be pointed to built-in commands so you can just issue the command directly.

Offline

 

#3 2021-06-09 05:59:18 am

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

Re: mkdir bash script embedded in Applescript issues

Here is some examples to do what you want...
https://macscripter.net/viewtopic.php?id=48153


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

Offline

 

#4 2021-06-09 03:39:30 pm

p_T
Member
Registered: 2021-06-09
Posts: 2

Re: mkdir bash script embedded in Applescript issues

Thanks for taking the time to look at this for me. Now working as i was hoping.

One observation is the \ in the string "FTC0000\ Job/{_Brief,Artwork,Dispatch/{Low\ Res,Hi\ Res/},History,Links,Supplied}" was actually intended to escape the space in FTC0000 Job directory.

Adding the extra \ to escape the \ makes the string work but the output is FTC0000 Job and not FTC0000\ Job and I am unsure why this is?

I have updated the string to use single quotes around directories with spaces.

Applescript:

tell application "Finder"
   set fp to (target of the front window as alias)
   set fp2 to POSIX path of fp
end tell

do shell script "mkdir -p " & quoted form of fp2 & "'FTC0000 Job'/{_Brief,Artwork,Dispatch/{'Low Res','Hi Res'/},History,Links,Supplied}"

Thanks for steering me in the right direction with this.

Offline

 

#5 2021-06-09 07:43:58 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 917

Re: mkdir bash script embedded in Applescript issues

p_T wrote:

Adding the extra \ to escape the \ makes the string work but the output is FTC0000 Job and not FTC0000\ Job and I am unsure why this is?



p_T. I took your script from post 4 and added back the double backslashes. On my computer--running Catalina with the default zsh shell--the folders created by the script included the backslashes. Mockman's script didn't work this way because it lacked the required single quotes. My knowledge in this area is limited, but I believe the single quotes are necessary to prevent the expansion of \\ by the shell, so that mkdir sees the two backslashes.


Applescript:

tell application "Finder"
   set fp to (target of the front window as alias)
   set fp2 to POSIX path of fp
end tell

do shell script "mkdir -p " & quoted form of fp2 & "'FTC0000\\ Job'/{_Brief,Artwork,Dispatch/{'Low\\ Res','Hi\\ Res'/},History,Links,Supplied}"

Last edited by peavine (2021-06-09 10:29:20 pm)


2018 Mac mini - macOS Catalina

Offline

 

#6 2021-06-10 02:39:47 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 45

Re: mkdir bash script embedded in Applescript issues

Thanks for fixing that. I guess I was even less familiar with the shell than I imagined. Actually, I didn't know what to expect from that folder block, as I've never used mkdir for that.

Offline

 

#7 2021-06-11 09:54:50 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 917

Re: mkdir bash script embedded in Applescript issues

The OP's question has been answered and I thought I would try this with ASObjC.

Applescript:

use framework "Foundation"
use scripting additions

tell application "Finder" to set baseFolder to target of front Finder window as text
set baseFolder to current application's NSString's stringWithString:(POSIX path of baseFolder)
set baseFolder to (baseFolder's stringByAppendingPathComponent:"FTC0000 Job")

set fileManager to current application's NSFileManager's defaultManager()

set theFolders to {"_Brief", "Artwork", "Dispatch/Hi Res", "Dispatch/Low Res", "History", "Links", "Supplied"}
repeat with aFolder in theFolders
   set theFolder to (baseFolder's stringByAppendingPathComponent:aFolder)
   (fileManager's createDirectoryAtPath:theFolder withIntermediateDirectories:true attributes:(missing value) |error|:(missing value))
end repeat

I ran some timing tests and there were no differences between the above script and that written by the OP in post 4. However, this assumes that the foundation framework is already in memory, which would normally be the case for me but not necessarily for the OP.

A backslash can be added to a folder name by using two backslashes as in the following:

Applescript:

set baseFolder to (baseFolder's stringByAppendingPathComponent:"FTC0000\\ Job")

Last edited by peavine (2021-06-11 10:26:52 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)