Move a file to a relative folder

Hi everyone,

I am new to this and english isn´t my native language, so sorry for any mistakes.

I am trying to move a selected file to a folder relative to it location. I have manage to make this work when moving the file to the desktop:

tell application “Finder”
set itemlist to the selection
set theFile to item 1 of itemlist
set theFile to (move theFile to desktop)
end tell

I would like now to replace desktop for something like ~/Final_Project , so I don´t need to tell the scrip where the location of the folder Final_Project is, just tell it that it is on the same location of the file selected, is there a way to accomplish this?

Thanks in advance.

I am relatively new to AppleScript, but I’ve managed to get this to work at moving a file to a Desktop folder named “Final Project”. You’ll need to change that portion of the script to match the location of your folder. Good luck.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
	activate
	set source_file to choose file with prompt "Please choose the Source File"
	set target_folder to "Users:homer:Desktop:Final Project" as alias --Change location as needed
	move source_file to (target_folder)
end tell
1 Like

Hi thanks for your help, but not exactly what I am looking for…

I need the script to move the selected file to a folder call Final_Project which it will be content within the same folder path of the file… but I need the scrip to use a relative path, as I have a lot of folder call Final_Project within many folders. so I cannot give it to the script a full path, just a relative path, to avoid to create as many script as folders I have.

A solution for this will be something like:
~/Final_Project The script need to understand that ~/ need to be replaced for the path of the file.
Not sure if the path of the file could be copy on a variable and then use the variable to replace ~/

Not sure if all of that make sense. I hope it is slight more clear.

Thanks

I’m afraid that what I posted represents pretty much the extent of my limited AppleScript knowledge. But, worry not, as there are a multitude of folks here that are “true experts” with AppleScript and have always assisted me when I was struggling with an issue. I’m sure that in just a couple of days you will have a solution to this issue.

Chavilan. If I understand your request correctly, the following should do what you want:

tell application "Finder"
	set theFiles to (selection as alias list)
	if theFiles is {} then display dialog "A file must be selected" buttons {"OK"} cancel button 1 default button 1
	set theFile to item 1 of theFiles
	set sourceFolder to container of theFile as text
	set targetFolder to sourceFolder & "Final Project:"
	if (exists folder targetFolder) is false then make new folder at sourceFolder with properties {name:"Final Project"}
	try
		move theFile to folder targetFolder
	on error
		display dialog "The selected file already exists in the target folder" buttons {"OK"} cancel button 1 default button 1
	end try
end tell

Since you’re only moving a single file, it’s simple to check the target folder for any item that might have the same name as the one to be relocated. This allows us to remove the try block:

if the name of thefile is in the name of items in the ¬
		folder named targetfolder then return false
	
move thefile to the folder named targetfolder
return true

On a separate note, it’s possible to simplify your implementation a bit by observing that the target folder is in the same containing directory as the selected files that are to be moved.

Finder’s selection will always be a list of file references that are housed in the same containing directory, and this directory will necessarily be the same as Finder’s insertion location. Therefore, your code can be reduced to the following:

tell application "Finder"
        set directory to "Final Project"
        set theFiles to the selection as alias list
        set sourceFolder to insertion location
        set targetFolder to a reference to the ¬
                sourceFolder's folder directory
        if not (exists the targetFolder) then make new ¬
                folder at sourceFolder with properties ¬
                {name:directory}
                move theFiles to the targetFolder replacing no
end tell

(It doesn’t actually look that reduced, but that’s my whacky obsessiveness over formatting.)

1 Like

FWIW, I tested both scripts on my system (Sonoma 14.7) and both worked perfectly (once I understood that the file had to actually be “selected” in Finder).

1 Like