Wednesday, November 22, 2017

#1 2017-09-21 07:13:06 pm

phongdsu
Member
Registered: 2017-02-09
Posts: 1

Creating and moving folder in Finder

Hello,
I am new to applescript. I am trying to create folder and subfolder and moving my selected folder in the subfolder. I am using "try" so if folder exist, it will not give me error.  Please help. Thanks. Here is my code:

tell application "Finder"
    try
        set folder1 to make new folder at folder "applescriptTest" of desktop with properties {name:"folder1"}
        set newFolderName to "folder2"
        set folderTemp to make new folder at folder1 with properties {name:newFolderName}
    end try
   
    set movingFolder to make new folder at folder "applescriptTest" of desktop with properties {name:"movingFolder"}
   
    move movingFolder to folderTemp
end tell

I got this error: "The variable folderTemp is not defined."

Offline

 

#2 2017-09-22 04:19:27 am

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

Re: Creating and moving folder in Finder

Hi. Welcome to MacScripter.

If an error occurs in a 'try' block, the script flow immediately jumps to the next instruction after the block. So if folder "folder1" already exists (or folder "applescriptTest" doesn't exist), none of the rest of the block is executed. The error occurs in the first line, so no attempt is made to create folder "folder2" and none of the variables are set. This is why the variable folderTemp doesn't exist when you try to move folder "movingFolder".

In a script which creates a nest of folders which may already exist, you have to test for each folder individually:

Applescript:

tell application "Finder"
   try
       set applescriptTest to folder "applescriptTest" of desktop
   on error
       set applescriptTest to (make new folder at desktop with properties {name:"applescriptTest"})
   end try
       
   try
       set folder1 to folder "folder1" of applescriptTest
   on error
       set folder1 to (make new folder at folder "applescriptTest" of desktop with properties {name:"folder1"})
   end try
   
   set newFolderName to "folder2"
   try
       set foldertemp to folder newFolderName of folder 1
   on error
       set foldertemp to (make new folder at folder1 with properties {name:newFolderName})
   end try
   
   set movingFolder to (make new folder at folder "applescriptTest" of desktop with properties {name:"movingFolder"})
   
   move movingFolder to foldertemp
end tell

Alternatively, there's a shell script you can use which does all the testing for you:

Applescript:

set desktopPath to POSIX path of (path to desktop)

-- Create the whole folder hierarchy as far as "folder1" if it doesn't already exist.
do shell script "mkdir -p " & quoted form of desktopPath & "applescriptTest/folder1/"

-- Now create the new folders.
tell application "Finder"
   set folder1 to folder "applescriptTest:folder1" of desktop
   
   set newFolderName to "folder2"
   try
       set foldertemp to folder newFolderName of folder 1
   on error
       set foldertemp to (make new folder at folder1 with properties {name:newFolderName})
   end try
   
   set movingFolder to (make new folder at folder "applescriptTest" of desktop with properties {name:"movingFolder"})
   
   move movingFolder to foldertemp
end tell

Obviously there are various ways to combine these methods. For instance, you could use the shell script to create the finished result with "movingFolder" already in "folder2", but I presume you're just experimenting with Finder commands at the moment.

Last edited by Nigel Garvey (2017-09-22 04:26:37 am)


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)