How to Reference Property in Finder Path

I am trying to make a finder path look at a property but not having success. I think there are backslashes and quotes involved since that is what you do to include quotes in a dialog but I am missing something here. Finder is looking at the folder as if it was named " & currentSemester & ".

property currentSemester : "fall14"

tell application "Finder"
	--This works
	set target of Finder window 1 to "Media:" & currentSemester & ":"
	
	--So how do I make Finder paths look at the property currentSemester
	delete (every document file of folder " & currentSemester & " of disk "Media")
end tell




Hello.

I haven’t tested this, but I think it should work.

property currentSemester : "fall14"

tell application "Finder"
	if (count of Finder windows) ≤ 0 then
		-- make new finder window
		beep
		return
	end if
	tell Finder window 1
		set its target to "Media:" & currentSemester & ":"		
		delete (every document of its target)
	end tell
end tell

That’s a reference, not a string.

Hey Skillet,

Why are you using a property instead of a variable? They have their uses of course, but it’s better to use a simple variable unless you have a specific need for a property.

This is not good practice, because you can never tell when an application will accept a plain path-string.

It’s better to turn the path-string into an actual object:


set _name to "wget_test"
set _parent to "Ryoko:Users:chris:Downloads:"
set _path to _parent & _name

tell application "Finder"
  # Good
  set myFolder to folder _path
  # Better
  set myFolder to _path as alias
end tell

Here you’re failing to understand string-concatenation '&". You’re trying to use concatenation within a Finder-Reference, and it just won’t fly.

You would write it like this:


set currentSemester to "fall14"
tell application "Finder"
	delete (every document file of folder currentSemester of disk "Athena")
end tell

But again this is a bad practice.

It’s better to do something like this:


set currentSemester to "fall14"
set targetFolderAlias to ("Athena:" & currentSemester) as alias

tell application "Finder"
	delete document files of targetFolderAlias
end tell

By coercing your path-string to an alias you have a built-in error-check, because it will error if the folder doesn’t exist.

You can also visualize the HFS path of the alias and see if it goes to the right place, and you can test this as well.

When you’re having failures you debug by commenting-out code and running to that point. If it succeeds you uncomment some more code, and run it - and so on. If it doesn’t succeed you comment-out some more code and run to that point.

If your code is too terse it gets more and more difficult to debug and generally isn’t enough faster than more descriptively verbose code to make it worthwhile.

Make your code as easy to read and understand as possible, and you’ll thank yourself a week, a month, or a year from now when you have to work on it again.

If I understand well, you inserted extraneous characters
Try :

property currentSemester : "fall14"

tell application "Finder"
	--This works
	set target of Finder window 1 to "Media:" & currentSemester & ":"
	
	--So how do I make Finder paths look at the property currentSemester
	delete every document file of (folder currentSemester of disk "Media")
end tell


I’m quite sure that the parenthesis are useless in the late instruction.

Yvan KOENIG (VALLAURIS, France) lundi 10 novembre 2014 10:58:17

Thank you all for the help to get this working. I had given up on this and am excited to have it working now. I am searching through all my posts for replies that I have missed since I just found one and found this. ccstone your detailed walkthrough was very helpful. I was using properties because I thought that was the best way to quickly update the macro at the start of each semester by seeing what needed to change at the top and didn’t realize it wasn’t good practice.