Select document name Folder inside subfolder

Hello,

How can we make a script that can open a folder with the name of a PDF document I choose on the desktop, put this PDF in that folder, and also open a subfolder named “incoming” in this folder and put it in the same pdf folder.

Thank you very much.

So:

Desktop: FileName

Hard Drive:Users:User:SomeFolder:FileName:

Hard Drive:Users:User:SomeFolder:FileName:incoming

You want to move or copy the pdf file (“FileName”) on the desktop to the folder “FileName” (located where?)

Then create folder named “incoming” folder the folder with the same name as the pdf file (“FileName”)

And then put something “it” in a pdf folder (which folder is that?)

All of this is doable, but I’m not clear exactly what you want here.

Thank you very much for your answer.

I will try to explain it like this (sorry for my poor English).

The script below does the first stage of the scenario I want. What I want after that is to open another folder named “incoming” in the resulting folder and copy the same first document to this folder named “incoming”.

At the end of the job two folders inside each other, and the document I selected in the first place.

It should be, thank you very much.

tell application "Finder"
   activate
   
   set selectedFiles to selection as alias list
   set containingFolder to container of (item 1 of selectedFiles) -- as alias
   
   repeat with i from 1 to count of selectedFiles
      
      set foldersRef to (a reference to folders of containingFolder)
      set foldersRefItems to name of (contents of foldersRef)
      set thisItem to item i of selectedFiles
      set fileName to (text items 1 thru -5) of (name of thisItem as text) as string
      
      if fileName is not in foldersRefItems then
         move thisItem to (make new folder at containingFolder with properties {name:fileName})
      else
         move thisItem to folder fileName of containingFolder
      end if
      
   end repeat
   
end tell
  1. Select demo.pdf
  2. Start script

Result:

Ekran Resmi 2023-03-29 12.31.47

By the way, if you put three backticks on the line before and after your code, like this below, it will be easier to work with.

```
  your code here
```

Assuming that you have a ‘demo’ folder on the desktop with demo.pdf inside it (and selected), this will create a new folder ‘gelen’ inside folder ‘demo’, and then copy ‘demo.pdf’ to that folder.

tell application "Finder"
	set fFile to selection -- demo.pdf that's in demo folder
	set fFolder to folder "demo" of desktop as alias
	
	set sFolder to make new folder at fFolder with properties {name:"gelen"}
	duplicate fFile to sFolder
	
end tell

Try this:

tell application "Finder"
   activate
   set selectedFiles to selection as alias list
   set containingFolder to container of (item 1 of selectedFiles) as alias
   repeat with i from 1 to count of selectedFiles
      set folderNames to (name of every folder of containingFolder)
      set thisItem to item i of selectedFiles
      set fileName to (items 1 thru -5) of (name of thisItem as text) as text
      if fileName is not in folderNames then
         set pdfFolder to (make new folder at containingFolder ¬
            with properties {name:fileName}) as alias
         duplicate thisItem to pdfFolder
      else
         set pdfFolder to folder fileName of containingFolder
         duplicate thisItem to folder fileName of containingFolder with replacing
      end if
      if not (exists folder "Incoming" of pdfFolder) then
         set incomingFolder to (make new folder at pdfFolder ¬
            with properties {name:"Incoming"}) as alias
      else
         set incomingFolder to folder "Incoming" of pdfFolder as alias
      end if
      move thisItem to incomingFolder
   end repeat
end tell

it works perfectly great, thank you very, very much, good luck to you and your work, I am very happy.

1 Like