Page 1 of 1

Help with choose file macro

Posted: 2009-05-28 09:06:41
by loulesko
I'm trying to get the code below to work, I keep getting an error saying that $file doesn't exists. I know it's simple, but if I could get a quikc example it would set me in the right direction to make more file moving macros that I need.

many thanks
Lou


$path = "~/Documents/Nisus Documents/"
$file = Choose File '$path', 'Move'
File.moveFromPathToPath '$file', ‘~/Documents/Writing/TrashFolder/'

Re: Help with choose file macro

Posted: 2009-05-28 11:42:31
by martin
Hi Lou, the problem is this line:

Code: Select all

$file = Choose File '$path', 'Move'
When you use single quote marks for a string/text, no variable interpolation occurs (replacing variable names with their content). Since $path is already a string, I'd recommend:

Code: Select all

$file = Choose File $path, 'Move'
But you could also use:

Code: Select all

$file = Choose File "$path", 'Move'
Hope that helps.

Re: Help with choose file macro

Posted: 2009-05-29 07:18:29
by Kino
You have to remove single quotes from '$file' in the last command too. Also there seems to be a misunderstanding. File.moveFromPathToPath requires a file path as the second argument. For example, if you want to move “MyLife.rtf”, the command will be...

Code: Select all

File.moveFromPathToPath '~/Documents/Nisus Documents/MyLife.rtf' '~/Documents/Writing/TrashFolder/MyLife.rtf' 

So your macro will be something like this:

Code: Select all

$path = '~/Documents/Nisus Documents/'
	# The path is enclosed between single quotes because it does not contain a variable.
$file = Choose File $path, 'Move'
$fileName = $file.lastFilePathComponent
File.moveFromPathToPath $file, "~/Documents/Writing/TrashFolder/$fileName"  
	# The path is enclosed between double quotes because it contains a variable ($fileName).
Edit:
• Perhaps it will be a nice enhancement if File.moveFromPathToPath works as you expected, i.e. move a file to a folder if the second argument is not a file path but an existing folder path.
• Corrected my English and added comments to the macro.

Re: Help with choose file macro

Posted: 2009-06-01 02:59:37
by loulesko
Thank you both. That helped a lot.

Lou