Thursday, July 9, 2020

#1 2020-06-28 03:20:02 am

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

Rename the selected file or folder with the parent name

Hi

I would like to rename any selected files or folders with the parent folder name. This is what I have so far but it's throwing up the following error:

Applescript:


error "Can’t set name of {«class docf» \"README.md\" of «class cfol» \"Design\" of «class cfol» \"(AMAN01)-0001, Mitsubishi Outlander 2015\" of «class cfol» \"Projects\" of «class cfol» \"Amanda Rockley ESMT (AMAN01)\" of «class cfol» \"Clients\" of «class cfol» \"Documents\" of «class cfol» \"daniel\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\"} to \"(AMAN01)-0001, Mitsubishi Outlander 2015\"." number -10006 from name of {«class docf» "README.md" of «class cfol» "Design" of «class cfol» "(AMAN01)-0001, Mitsubishi Outlander 2015" of «class cfol» "Projects" of «class cfol» "Amanda Rockley ESMT (AMAN01)" of «class cfol» "Clients" of «class cfol» "Documents" of «class cfol» "daniel" of «class cfol» "Users" of «class sdsk»}

Applescript:


--> GET PROJECT NAME
--> VERSION 1


--> Get Current Folder Path (CurrentPath) and Name (CurrentFolder)
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 path
   set CurrentPath to POSIX path of (CurrentDir)
end tell

set ParentFolderName to my ParentFolderExtraction(CurrentPath)


set theSel to (selection of application "Finder")

if theSel is not {} then
   display dialog "file selected" & " " & ParentFolderName
   
   set name of theSel to ParentFolderName
   --display dialog "Please select a file you wish to change the name of." buttons {"OK"} default button "OK" with icon caution --or use icon stop    
else
   display dialog "file not selected"
   --set the name of file sel to ParentFolderExtraction
end if

#===== Now we aren't in the main code, we may put the handlers

on ParentFolderExtraction(ParentObject)
   tell application "System Events"
       return name of container of disk item (path of disk item (ParentObject as text))
   end tell
end ParentFolderExtraction

Offline

 

#2 2020-06-28 04:50:41 am

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

Re: Rename the selected file or folder with the parent name

The error message is perfectly clear which warn you that you can't give a name to a list of file(s).

Edit one instruction and it will work:

Applescript:

set name of (item 1 of theSel) to ParentFolderName

I'm not sure that it's a good idea to rename a file (with an extension like your “README.md”) to the name of a folder (without an extension).
Here the file originally named “README.md” would be renamed “(AMAN01)-0001, Mitsubishi Outlander 2015”.

In precedent messages, I showed you that your handler is unnecessarily long.
It may be replaced by:

Applescript:

on ParentFolderExtraction(ParentObject)
   tell application "System Events"
       return name of container of disk item ParentObject
   end tell
end ParentFolderExtraction

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 28 juin 2020 12:38:16

Last edited by Yvan Koenig (2020-06-28 04:56:22 am)

Offline

 

#3 2020-06-28 05:08:02 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 576

Re: Rename the selected file or folder with the parent name

Applescript:

set name of (item 1 of theSel) to ParentFolderName

I would like to point out to the OP that a Finder selection contains a list - always.
But the amended code still fails. The script must talk to Finder or System Events to deal with the file system.

I agree with Yvan's other point. Not a good idea to rename files without respecting their extension.

And yet another point: if no Finder window is open it'll get the name of the user's home folder. That may not be what the OP wanted.

I also urge Danjuan to read the relevant dictionaries.

Offline

 

#4 2020-06-28 07:31:52 am

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

Re: Rename the selected file or folder with the parent name

alastor933 wrote:

Applescript:

set name of (item 1 of theSel) to ParentFolderName

I would like to point out to the OP that a Finder selection contains a list - always.
But the amended code still fails. The script must talk to Finder or System Events to deal with the file system.


I don't know if it fails with your system but it doesn't with mine.

The file was renamed from "blahblah.mg" to the name of my home folder :"**********"
My understanding is that we aren't working upon a standard reference to a file but upon a Finder's one.
If I edit one instruction as:

Applescript:

set theFile to (item 1 of theSel) as alias
   set name of theFile to ParentFolderName

the script fail with an obscure message which doesn't describe what is really wrong:
error "Saturation de la pile." number -2706
although we are targeting an object which we aren't allowed to work with.

I agree with Yvan's other point. Not a good idea to rename files without respecting their extension.

And yet another point: if no Finder window is open it'll get the name of the user's home folder. That may not be what the OP wanted.

I also urge Danjuan to read the relevant dictionaries.


In fact, it's the entire scheme which is odd.
When I ran my example above, the open window was the Documents one which was in list mode so I navigated the hierarchy:
Documents
—Clients
——Amanda Rockley ESMT (AMAN01)
———Projects
————(AMAN01)-0001, Mitsubishi Outlander 2015
—————Design
——————blablah.md

The container of this Finder window is my home folder and the file was renamed according to it : **********

I made an other attempt.
This time the file was selected in the Finder window "Design" in mode Icon. The file was renamed as "(AMAN01)-0001, Mitsubishi Outlander 2015" which is the name of the container of the Finder window "Design".
With the code design we never know what will be used to rename the file except if we always work upon window in mode Icon.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 28 juin 2020 15:31:16

Last edited by Yvan Koenig (2020-06-28 08:07:29 am)

Offline

 

#5 2020-06-29 01:45:13 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 576

Re: Rename the selected file or folder with the parent name

Yvan Koenig wrote:
alastor933 wrote:

Applescript:

set name of (item 1 of theSel) to ParentFolderName

I would like to point out to the OP that a Finder selection contains a list - always.
But the amended code still fails. The script must talk to Finder or System Events to deal with the file system.

I don't know if it fails with your system but it doesn't with mine.

That's odd. Sounds like a bit of handholding by AppleScript. And, IMHO, it encourages sloppy coding. A novice scripter will have a hard time understanding why his code sometimes fails on this point.

Yvan Koenig wrote:

With the code design we never know what will be used to rename the file except if we always work upon window in mode Icon.

I totally agree. I'd go further: scripts like this should only run with one item selected in the front Finder window.

Offline

 

#6 2020-06-29 04:57:16 am

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

Re: Rename the selected file or folder with the parent name

I completely agree about the extension. My system has extensions hidden so I just assumed it would leave that part alone, obviously not.

Offline

 

#7 2020-06-29 07:13:18 am

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

Re: Rename the selected file or folder with the parent name

alastor933 wrote:
Yvan Koenig wrote:
alastor933 wrote:

Applescript:

set name of (item 1 of theSel) to ParentFolderName

I would like to point out to the OP that a Finder selection contains a list - always.
But the amended code still fails. The script must talk to Finder or System Events to deal with the file system.

I don't know if it fails with your system but it doesn't with mine.

That's odd. Sounds like a bit of handholding by AppleScript. And, IMHO, it encourages sloppy coding. A novice scripter will have a hard time understanding why his code sometimes fails on this point.


I repeat that there is nothing wrong here.
The code do correctly what it's supposed to do.
This said  this scheme has  side effect as you may see in this edited/commented version

Applescript:


--> GET PROJECT NAME
--> VERSION 1

property usePOSIXPath : false
property wrongScheme : false
--
--> Get Current Folder Path (CurrentPath) and Name (CurrentFolder)
tell application "Finder"
   if exists Finder window 1 then
       set CurrentDir to target of Finder window 1 as string --> "SSD 1000:Users:yvankoenig:Desktop:dossier / sans titre:dossier sans titre2:"
   else
       set CurrentDir to (desktop as string)
   end if
   --> Get Current Folder path
   if usePOSIXPath then
       set CurrentPath to POSIX path of (CurrentDir)
   else
       set CurrentPath to CurrentDir
   end if
end tell
-- CAUTION, as we use System Events to get the name of the container,
-- the embedded slash is replaced by a colon
if wrongScheme then set ParentFolderName to my ParentFolderExtraction(CurrentPath) --> "dossier : sans titre"

set theSel to (selection of application "Finder") --> {document file "version 1 copie 2.scpt" of folder "dossier sans titre2" of folder "dossier / sans titre" of folder "Desktop" of folder "yvankoenig" of folder "Users" of startup disk}

if theSel is not {} then
   if wrongScheme then display dialog "file selected" & " " & ParentFolderName --> "file selected dossier : sans titre"
   set aRef to item 1 of theSel
   log aRef (*document file version 1 copie 2.scpt of folder dossier sans titre2 of folder dossier / sans titre of folder Desktop of folder yvankoenig of folder Users of startup disk*)
   set itsContainer to my getItsContainer(aRef) --> folder "dossier sans titre2" of folder "dossier / sans titre" of folder "Desktop" of folder "yvankoenig" of folder "Users" of startup disk
   if not wrongScheme then
       set ParentFolderName to name of my getItsContainer(itsContainer) --> "dossier / sans titre"
   end if
   set oldName to name of aRef --> "version 1 copie 2.scpt"
   set name of aRef to ParentFolderName --> "dossier : sans titre" or "dossier / sans titre"
   log aRef (*document file version 1 copie 2.scpt of folder dossier sans titre of folder Desktop of folder yvankoenig of folder Users of startup disk*)
   try
       log (get name of aRef)
   on error errMsg number errNbr
       log errMsg (*Erreur dans Finder : Il est impossible d’obtenir document file "version 1 copie 2.scpt" of folder "dossier sans titre2" of folder "dossier / sans titre" of folder "Desktop" of folder "yvankoenig" of folder "Users" of startup disk.*)
   end try
   tell application "Finder" to tell itsContainer
       exists (file 1 whose name is ParentFolderName) --> true
   end tell
else
   display dialog "file not selected"
   --set the name of file sel to ParentFolderExtraction
end if

#===== Now we aren't in the main code, we may put the handlers

on ParentFolderExtraction(ParentObject) -- used only if wrongScheme is true
   tell application "System Events"
       return name of container of disk item (ParentObject as text)
   end tell
end ParentFolderExtraction

on getItsContainer(aRef)
   tell application "Finder"
       return container of aRef
   end tell
end getItsContainer

It's sad because when Danjuan asked for the way to get the name of the container of the container of a file, I gave it a correct answer which he didn't use.

Yvan Koenig wrote:

With the code design we never know what will be used to rename the file except if we always work upon window in mode Icon.

I totally agree. I'd go further: scripts like this should only run with one item selected in the front Finder window.



It's not specific to this kind of scripts.
Every script grabbing it's entry data from a selection but is supposed to treat a single item must work upon item 1 of the selection and, if several items are selected it may get the wanted result or a wrong one.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 29 juin 2020 15:08:45

Last edited by Yvan Koenig (2020-06-29 08:27:04 am)

Offline

 

#8 2020-06-30 02:35:53 am

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

Re: Rename the selected file or folder with the parent name

Thank you for your help.

You are right, your answer does do exactly what I asked so having thought more about what I need, I will create new post to avoid confusion.

It's a shame you can't mark the answer as complete like stack overflow.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)