Drag and drop files as object

I d like to have the dragged file as an applescript object.

on drop theObject drag info dragInfo
	set dataTypes to types of pasteboard of dragInfo
	if "file names" is in dataTypes then
		set preferred type of pasteboard of dragInfo to "file names"
		set thePaths to contents of pasteboard of dragInfo 
		set preferred type of pasteboard of dragInfo to ""	
		set thePath to item 1 of thePaths 
		set thePath to (POSIX file thePath)
	end if
end drop

thePath is liek User:folderfile:filename and i d like to have it the form file filename of folderfile of folder user.

How could i proceed

Thanks for your help.

Hi,

why do you want this form? The form file filename of folderfile of folder user is only used by the Finder.
An alias is independent from the Finder and much more flexible (considers renaming and moving)

set thePath to (POSIX file thePath) as alias

Thanks again Stefan for your quick answer.

I am improving my script using the drag and drop function.

I d like to avoid using the finder for my script being faster.

set text item delimiters to ":"
set fileName to last text item of (dragfile as text)
set file_folder to (((text items 1 thru -2 of (dragfile as text)) as text) & ":") as alias
set AppleScript's text item delimiters to tid

I d like to obtain the modification date of this file

i can do this :

tell application “Finder”
set this_file to file fileName of folder file_folder
end tell
set dateObject to modification date of this_file
set theDate to date string of dateObject
set thetime to time string of dateObject

But it squite long maybe you know another solution.
`
Thanks

Try this, it assumes that dragfile is an alias


set dragfile to alias "MacHD:Path:to:File"
set {tid, text item delimiters} to {text item delimiters, ":"}
tell (dragfile as text) to set {fileName, file_folder} to {text item -1, (text items 1 thru -2 as text) as alias}
set text item delimiters to tid
set dateObject to modification date of (info for dragfile)

Thanks again stefan,

Still in the process of improving my script speed.

Is there a way to avoid unsing the “finder” to know if a file or a folder exists


tell application "Finder"
		if folder "myfolder" of resource_folder  exists then
		else
		make new folder at resource_folder with properties {name:"myfolder"}
                end if
end tell

Cause I find it a little slow.

this creates the directory if it doesn’t exist


do shell script "/bin/mkdir -p " & quoted form of (POSIX path of resource_folder & "myFolder")

if resource_folder is a HFS string path, it must end with “:”

Thanks again.

Is there a way to know if a file or a folder already exists without using the Finder with a unix command.

Thanks

Francois

The Finder is not needed at all to get the POSIX path of an alias


POSIX path of (path to applications folder) --> "/Applications/"

To check a valid path, you can use this example


set iTunesHFSPath to (path to applications folder as Unicode text) & "iTunes.app"
checkPath(iTunesHFSPath)

on checkPath(P)
	try
		P as alias
		return true
	on error
		return false
	end try
end checkPath

As i can see you have some time today i ll ask you some more questions and thanks you agin for everything.

this is what i used to do

tell application "Finder"
			set monFichier to (file myfile of folder foldertxt of folder "Settings" of resource_folder) as alias
		end tell
		open for access monFichier
		read monFichier

now i want to remove the finder and i do that:

open for access ((the POSIX path of (resource_folder)  & "Settings" & "/" & foldertxt & "/" & myfile) as Unicode text) as alias
	read monFichier

but does not work of course

In most cases it’s not necessary to open the file for access to read a text file,
unless you want to read a hugh file in portions to avoid memory overflow.

this is sufficient


read alias (resource_folder & "Settings:" & foldertxt & ":" & myfile)
-- or
read file (resource_folder & "Settings:" & foldertxt & ":" & myfile)

Note: AppleScript’s read/write commands require always a HFS path,
in the example the variable resource_folder must be a HFS path with a colon at the end

It s ok i found


	set monFichier to (((resource_folder) as Unicode text)  & "Settings" & ":" & foldertxt & ":" & myfile) as alias
	open for access monFichier
	read monFichier

you can omit the open for access line,
otherwise you have to close the file again

Thanks i did not know that for the open access

Ok so now my function runonethumb() works perfectly without using the “Finder”

Now i am able to drag and drop files in that


on drop theObject drag info dragInfo
	set dataTypes to types of pasteboard of dragInfo
	if "file names" is in dataTypes then
		set preferred type of pasteboard of dragInfo to "file names"
		set thePaths to contents of pasteboard of dragInfo
		repeat with thePath in thePaths
			set dragfile to (POSIX file thePath) as alias
			runonethumb()
		end repeat
		set preferred type of pasteboard of dragInfo to ""
	end if
end drop

Now what i dlike to do is to be able to drag folders and launch the runonethumb() function for every file inside the folder and subfolders

Another question i could i list every file from a folder as alias witout using the finder


tell application "Finder"
set impfolder to every file of entire contents of folder thePathalias
end tell

to get all files of entire contents is only possible
with the shell commands find or mdfind (spotlight)
here an example for find


set theFolder to quoted form of POSIX path of (choose folder)
set theFiles to paragraphs of (do shell script "/usr/bin/find " & theFolder & " -type f  ! -name  '.*'") -- finds files whose name doesn't start with a dot
repeat with oneFile in theFiles
	set oneFileAlias to POSIX file oneFile as alias
	-- do something
end repeat

This what i did


set my_doc_list to (do shell script "ls " & "\"" & the POSIX path of thePathalias & "\"")
set AppleScript's text item delimiters to return
set itemCount to count of text items in my_doc_list
repeat with i from 1 to itemCount
set currentItem to (text item i of my_doc_list) as string
copy (((thePathalias as Unicode text) & currentItem) as alias) to end of myVideoList
end repeat

it works very well

now i d like to repeat in myVideoList from 10 to 15 for example

let s think about it

easier


set my_doc_list to paragraphs of (do shell script "ls " & quoted form of POSIX path of thePathalias)

set myVideoList to {}
repeat with i in my_doc_list
	copy ((thePathalias as Unicode text) & i) as alias to end of myVideoList
end repeat

but in this case I’d prefer to use the Finder
in Leopard this works


tell application "Finder" to set myVideoList to files of thePathalias as alias list

in Tiger there’s a bug, therefore you have to catch an error


tell application "Finder"
	try
		set myVideoList to files of thePathalias as alias list
	on error
		set myVideoList to files of thePathalias as alias as list
	end try
end tell

you can define an range in a repeat loop with

repeat with i from 10 to 15
	get item i of myVideoList
	-- do something
end repeat

or the last 5 items


repeat with i from -5 to -1
	get item i of myVideoList
	-- do something
end repeat

Nice but i ll keep my solution

I just made an if on the indice and it s ok

Now i have a data table and i want to get the row number of the selected data row

there must be a call method but i don t find it


repeat with this_row in selected_rows
set row_index to call method ???"indexOfObject:"??? of this_row
display dialog row_index
delete this_row
end repeat

The you have to reset (to {“”}) the text item delimiters.
Every coercion of a list to a string will consider text item delimieters

table view has a property selected row, which returns the integer index of the selected row