Saturday, March 28, 2020

#1 2020-03-25 03:58:34 am

rsommerer
Member
Registered: 2020-03-21
Posts: 2

Choose Folders and pass them on to another functionality

Hello experts,

maybe this question has been raised before, but I couldn´t find any specific postings. So I started this new topic, hoping you will be able to help me out of my misery.

What I am trying to do is create a little backup program for my most important folders. I want it to be flexible, so I would like to implement a choose folder dialog (only folders are involved). Here is what I have so far:

Applescript:



script DelOldBck
   
   on DelOldBck()
       
       set OldDefault to alias "Macintosh HD:Users:XXX:Desktop:"
       
       set OldFolders to (choose folder with prompt "Choose Folders to delete:" default location OldDefault with multiple selections allowed)
       
       tell application "Finder" to set theOLdFold to every folder of OldFolders as alias list
       
       repeat with _folder in theOLdFold
           
           delete _folder
           
       end repeat
       
   end DelOldBck
   
end script

tell DelOldBck to DelOldBck()

So basically I would like to select something which then gets passed on to another functionality, in this particular case the trash. But it might as well be a functionality to duplicate the selected folders to a different location instead of the trash. Or the bin, as you may call it..

I have done some research on POSIX, etc. and have already done some testing with different setups in this regard. Everything failed and threw an error, the above coding is the one that does not throw any errors, but doesn´t do anything either.

From what I have seen in the standard ScriptEditor, the selection is stored as an array of aliases, does that have anything to do with the problems that arise here? I am working with Catalina by the way.

Please share your knowledge on this one. If you have a solution I would also be interested why your code works. Thanks in advance!


Filed under: Dialog, conversion, folder, choose

Offline

 

#2 2020-03-25 04:37:00 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1013

Re: Choose Folders and pass them on to another functionality

Like this?

Applescript:


script DelOldBck
   
   property backupFolder : path to temporary items
   property OldDefault : path to desktop folder
   property OldFolders : {}
   
   on DelOldBck()
       set OldFolders to (choose folder with prompt "Choose Folders to delete:" default location OldDefault with multiple selections allowed)
       repeat with _folder in OldFolders -- repeat with every folder of the list OldFolders
           tell application "Finder" to delete _folder -- tell to Finder to delete current alias (send to trash)
       end repeat
   end DelOldBck
   
   on emptyTrash()
       tell application "Finder" to empty trash
   end emptyTrash
   
   on backupFolders()
       tell application "Finder" to duplicate _folder to backupFolder replacing yes
   end backupFolders
   
end script

DelOldBck's DelOldBck()
-- DelOldBck's emptyTrash() -- uncomment to empty the trash
-- DelOldBck's backupFolders() -- uncomment to backup the folders
return {DelOldBck's backupFolder, DelOldBck's OldDefault, DelOldBck's OldFolders}

Now, replace the code after end script with this and note what happens. The script (that is, script object named "DelOldBck") changes little the behaviour, when you change it's properties:

Applescript:


set DelOldBck's OldDefault to downloads folder
DelOldBck's DelOldBck()

Last edited by KniazidisR (2020-03-25 06:07:24 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4, Ram 4 GB
Safari -- version 12.1
Firefox -- version 70.0

Online

 

#3 2020-03-25 01:54:44 pm

rsommerer
Member
Registered: 2020-03-21
Posts: 2

Re: Choose Folders and pass them on to another functionality

What a quick response, I think I understand what you are doing here.
Besides, everything you stated worked fine in my tests, thanks a lot!

There is one issue though that I couldn´t catch with for example a try statement. If in the choose folder dialog I select folders all the rest runs fine. If I don´t select anything AppleScript throws an error and quits, do you have any thoughts on how to keep the program running?

And one last question on your return remark. In my tests I could use variables defined in a particular script only in that script. So I assume that returning the variables makes them reusable again in any other script that attempts to use them? That would be a great advantage, I just did not have any time yet to test that. It would make the variables accessible on a global level.

Offline

 

#4 2020-03-25 11:26:18 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1013

Re: Choose Folders and pass them on to another functionality

rsommerer wrote:

There is one issue though that I couldn´t catch with for example a try statement. If in the choose folder dialog I select folders all the rest runs fine. If I don´t select anything AppleScript throws an error and quits, do you have any thoughts on how to keep the program running?


You can do something like this:

Applescript:


script DelOldBck
   
   property backupFolder : path to temporary items
   property OldDefault : path to desktop folder
   property OldFolders : {}
   
   on DelOldBck()
       try
           set OldFolders to (choose folder with prompt "Choose Folders to delete:" default location OldDefault with multiple selections allowed)
           repeat with _folder in OldFolders -- repeat with every folder of the list OldFolders
               tell application "Finder" to delete _folder -- tell to Finder to delete current alias (send to trash)
           end repeat
       on error number -128
           display notification "Errorr number -128: User cancelled. Only This call will terminated. Not the next"
       end try
   end DelOldBck
   
   on emptyTrash()
       tell application "Finder" to empty trash
   end emptyTrash
   
   on backupFolders()
       tell application "Finder" to duplicate _folder to backupFolder replacing yes
   end backupFolders
   
end script

DelOldBck's DelOldBck() -- Cancelled
DelOldBck's emptyTrash() -- Executed

rsommerer wrote:

And one last question on your return remark. In my tests I could use variables defined in a particular script only in that script. So I assume that returning the variables makes them reusable again in any other script that attempts to use them? That would be a great advantage, I just did not have any time yet to test that. It would make the variables accessible on a global level.


You can get value of variable Var1 of script A into variable passedValue of script B. To be this available Var1 should be declared as property of  script A.

Applescript:


script A
   property Var1 : "I am special variable of script A"
end script

script B
   on passVariable()
       set passedValue to A's Var1
   end passVariable
end script

return B's passVariable()

You can return passVariable not using B's method (as in the previous example), but using B's property as well:

Applescript:


script A
   property Var1 : "I am special variable of script 1"
end script

script B
   property passedValue : A's Var1
end script

return B's passedValue

Last edited by KniazidisR (2020-03-25 11:52:40 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4, Ram 4 GB
Safari -- version 12.1
Firefox -- version 70.0

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)