Check if file is already on server

Hi, i have this this script that checks if a file already exists on the server. It works when i use a ‘static’ script:


tell application "Finder"
	activate
	if file "MAC_SERVER:My_Folder:01.doc" exists then
		display dialog "This file already exists"
	end if
end tell

Little bit useless code, because you can’t choose a file, so i want a droplet, that checks if the dropped file exists:


on open (q_file)
	tell application "Finder"
		activate
		if file "MAC_SERVER:My_Folder:" & q_file exists then
			display dialog "This file already exists"
		end if
	end tell
end open

Can somebody tell me what i’m doing wrong here?? There’s something wrong with the concatenation…but what!!! Please help me!

Grtz, Fuut

Hi :slight_smile:
You can also use the “as alias” method, to know some more, see a “File/Folder Check” routine of Jon…
:wink:

Hi thnx! for the reply! :smiley:

I took a look at Jons code, and it works, but it isn’t a droplet, so you still have to type the full filename.

Just to be sure my wish is clear to you :smiley: :

i drop a file on my own client Mac on the droplet and it checks if that file is present on the server in a certain folder, that folder is always the same folder, well, for now at least :wink: :wink: And if it exists it has to show a dialog instead of a line in the result-window…So can you maybe suggest litte adjustments to Jons code?? I’m pretty new to this, so that’s why… :rolleyes:

Thnx IA,

Fuut

I think this is what you want:

property target_folder : "MAC_SERVER:My_Folder:"

on run
	display dialog "Drop a file on this application."
end run

on open the_items
	set old_delim to AppleScript's text item delimiters
	repeat with this_item in the_items
		set this_item to this_item as string
		set AppleScript's text item delimiters to ":"
		if this_item ends with ":" then
			set the_name to text item -2 of this_item
		else
			set the_name to text item -1 of this_item
		end if
		set AppleScript's text item delimiters to old_delim
		set f_to_check to (target_folder & the_name)
		if f_exists(f_to_check) = true then
			display dialog "The item " & f_to_check & " exists."
		else
			display dialog "The item " & f_to_check & " does not exist."
		end if
	end repeat
end open

on f_exists(the_path)
	try
		get the_path as alias
		return true
	on error
		return false
	end try
end f_exists

Jon

Thanks Jon! :smiley: It works great! The next step is, that is also looks for the file in the subfolders…I’ve been looking around on BBS, and the code to be added should look something like this:


try 
    set theFolderList to every folder of folder target_folder 
    repeat with subFolder in theFolderList 
....

I tried for hours to get this to work with your code, but all i get is error after error. So i’m afraid i’m gonna ask you if you can help me with this please! :oops:

Any tip is welcome!

Thnx! Fuut

Save the following code as an application, it will work with subfolders:

property target_folder : "MAC_SERVER:My_Folder:"
property found_f : false
property found_f_path : ""

on run
	display dialog "Drop a file on this application."
end run

on open the_items
	set old_delim to AppleScript's text item delimiters
	repeat with this_item in the_items
		set this_item to this_item as string
		set AppleScript's text item delimiters to ":"
		if this_item ends with ":" then
			set the_name to text item -2 of this_item
		else
			set the_name to text item -1 of this_item
		end if
		set AppleScript's text item delimiters to old_delim
		set {found_f, found_f_path} to {false, ""}
		my check_f(the_name, target_folder)
		if found_f = true then
			display dialog "The item " & the_name & " was found in the folder " & found_f_path & "."
		else
			display dialog "The item " & the_name & " was not found."
		end if
	end repeat
end open

on check_f(the_name, the_folder)
	if found_f = true then return
	set f_to_check to (the_folder & the_name)
	if f_exists(f_to_check) = true then
		set found_f_path to the_folder
		set found_f to true
	else
		tell application "Finder" to set the_subs to folders of folder the_folder
		repeat with this_sub in the_subs
			my check_f(the_name, (this_sub as string))
		end repeat
	end if
end check_f

on f_exists(the_path)
	try
		get the_path as alias
		return true
	on error
		return false
	end try
end f_exists

Jon

Thank you so much! It works, maybe a little slow…but heee, who cares! Grtzzzz!!!

I wanted to point out the reason that your orignal attempt didn’t work. The variable passed into the droplet is the full file path of the file, not just the name. The way I usually deal with this is to cast the path to an alias, then you can use finder to get the name of the file that the alias points to. You could also parse the file path to get just the name (find the position of the last “:” and then take everything after that).