Help! Folder Action Script

I am a graphic artist. I know enough about scripting to get by but I far far
from an expert.

i am sure this is really simple, but its beyond me…

description:

when an item (text doc) is placed into a folder: text-edit plus will activate
and search said document for a specific term (LCLPGAD)

the script will then report/display to the user the number of times it finds
the term - prompting the user to take further action with “OK” or “Cancel”

if “OK” the script would then activate the FTP program (Yummy FTP) , telling it
to use a specified bookmark and then upload the file - the dialog disappears

if “Cancel” nothing happens, the dialog disappears, everything closes and the
script ends

_

this where i am at (needless to say it doesnt compile - think i have the
text-edit portion correct it the upload part that is wrong or the prompt that is
wrong…i dunno - i havnt done the cancel portion)

i am clueless…don’t laugh : ( thank you in advance

on adding folder items to this_folder after receiving added_items

tell application "Tex-Edit Plus" 
	set y to "LCLPGAD" 
	set numwins to (get number of windows) 
	set countr to 0 
	set loopflag to false 
	set x to 1 
	 
	repeat until x = (numwins + 1) 
		 
		repeat 
			if loopflag is false then 
				search window x looking for y with whole words matching and cases matching 
				copy result to sresult 
			end if 
			 
			if loopflag is true then 
				search window x looking for y with searching from cursor, whole words 

matching and cases matching
copy result to sresult
end if

			if true is in the sresult then 
				copy countr + 1 to countr 
				set loopflag to true 
				select insertion point after selection of window x 
			end if 
			 
			if false is in the sresult then exit repeat 
		end repeat 
		 
		copy x + 1 to x 
		set loopflag to false 
	end repeat 
	 
	set time_ to " times in " 
	if countr = 1 then set time_ to " time in " 
	set doc_ to " documents" 
	if numwins = 1 then set doc_ to " document." 
	 
	display dialog "Found term:" & return & return & "   " & y & return & return & 

countr & time_ & numwins & doc_ buttons {“Cancel”, “OK”} default button “OK”
with icon note
if button returned is equal to “OK” then
tell application “Finder”
activate “Yummy FTP”
end tell

tell application “Yummy FTP”
connect “zap”
end tell

repeat with this_item in added_items 
	tell application "Yummy FTP" 
		using connection "zap" upload this_item 
	end tell 
end repeat 
 
tell application "Yummy FTP" 
	disconnect "zap" 
end tell 

end adding folder items to

Browser: Safari 523.12
Operating System: Mac OS X (10.4)

And now that I have configured the tex-edit portion as a folder action script…i can see that it doesnt actually scan the entire docuument

but rather…the doc. needs to be open with tex-deit and a cursour positioned…

ugh all i want it to do is go line by line…looking for that data

sawtooth:

Depending on how large your text file is, here’s a quick shell for finding the search term (with comments).

set searchString to "LCLPGAD" -- Caching it to a variable for ease of use
set testDoc to ((path to desktop) & "LCLPGAD_Test.txt" as Unicode text) as alias -- Here's a fake file on my Desktop with 6 occurrences of LCLPGAD
-- This can be replaced with a Choose File or a droplet, etc.

set oldTID to AppleScript's text item delimiters -- Cache the old delimiters
set bodyContent to (read testDoc) as Unicode text -- Reading the textfile
set wordCount to count words of bodyContent -- Count the number of words in the doc (actaully stored in the variable bodyContent)
set AppleScript's text item delimiters to searchString -- Here's what we're looking for
set bodyContent to (text items of bodyContent) -- Get the text items from the doc but ignore our search string. This returns a list of text items
set AppleScript's text item delimiters to " " -- Replace the search string with spaces so we don't end up with one long word
set bodyContent to bodyContent as Unicode text -- Put the words back together
set wordCount2 to count words of bodyContent -- Count the words again
set AppleScript's text item delimiters to oldTID -- Reset the delimiters
set foundWordCount to wordCount - wordCount2 -- Determine the difference between the two counts and there's your number of occurrences.
display dialog searchString & " found " & foundWordCount & " times" -- And it does indeed report 6 occurrences.

Hope this helps a little.

Have fun,
Jim Neumann
BLUEFROG

thank you…i will see what i can do with it…lol

i am just a dumb graphic artist : ) lol

i was able to compile it and created a fake doc…yep that work…gave me back 6 instances

so, i guess my next step would be to see if i could turn that…into a folder action script…so that when something is dropped into a select folder it will launch the script on its contents)

should be easy enough : )) (yeah right…who am i kidding …lol)

mr. t

_ update_ ok that was easy (sorry staples)

_ now i just have to figure out… “ok” and “cancel” - ok will upload the document to a deafult ftp location and cancel will …well it will just cancel

on adding folder items to this_folder after receiving added_items
set searchString to “LCLPGAD” – Caching it to a variable for ease of use
set testDoc to added_items as Unicode text as alias

set oldTID to AppleScript's text item delimiters -- Cache the old delimiters
set bodyContent to (read testDoc) as Unicode text -- Reading the textfile
set wordCount to count words of bodyContent -- Count the number of words in the doc (actaully stored in the variable bodyContent)
set AppleScript's text item delimiters to searchString -- Here's what we're looking for
set bodyContent to (text items of bodyContent) -- Get the text items from the doc but ignore our search string. This returns a list of text items
set AppleScript's text item delimiters to " " -- Replace the search string with spaces so we don't end up with one long word
set bodyContent to bodyContent as Unicode text -- Put the words back together
set wordCount2 to count words of bodyContent -- Count the words again
set AppleScript's text item delimiters to oldTID -- Reset the delimiters
set foundWordCount to wordCount - wordCount2 -- Determine the difference between the two counts and there's your number of occurrences.
display dialog searchString & " found " & foundWordCount & " times" -- And it does indeed report 6 occurrences.

end adding folder items to

ewwwww look at me go!! :smiley:

the only problem is that after Yummy uploads…Yummy “quits unexpectedly”:frowning:

i wonder i can get this working with cyber duck??

ok here is what i have…can anyone see anything wrong???




on adding folder items to this_folder after receiving added_items
	set searchString to "LCLPGAD" -- Caching it to a variable for ease of use
	set testDoc to added_items as Unicode text as alias
	
	set oldTID to AppleScript's text item delimiters -- Cache the old delimiters
	set bodyContent to (read testDoc) as Unicode text -- Reading the textfile
	set wordCount to count words of bodyContent -- Count the number of words in the doc (actaully stored in the variable bodyContent)
	set AppleScript's text item delimiters to searchString -- Here's what we're looking for
	set bodyContent to (text items of bodyContent) -- Get the text items from the doc but ignore our search string. This returns a list of text items
	set AppleScript's text item delimiters to " " -- Replace the search string with spaces so we don't end up with one long word
	set bodyContent to bodyContent as Unicode text -- Put the words back together
	set wordCount2 to count words of bodyContent -- Count the words again
	set AppleScript's text item delimiters to oldTID -- Reset the delimiters
	set foundWordCount to wordCount - wordCount2 -- Determine the difference between the two counts and there's your number of occurrences.
	display dialog searchString & " found " & foundWordCount & " times" -- And it does indeed report 6 occurrences.
	if the button returned of the result is "OK" then
		tell application "Yummy FTP"
			connect "PBS EXCHANGE"
		end tell
		
		repeat with this_item in added_items
			tell application "Yummy FTP"
				using connection "PBS EXCHANGE" upload this_item
			end tell
		end repeat
		
		tell application "Yummy FTP"
			disconnect "PBS EXCHANGE"
		end tell
	end if
	
end adding folder items to


You can also use cURL instead of any third party tool with this syntax


do shell script "/usr/bin/curl 'ftp://myServer.com/path/to/destination/file.ext' -u user:pass -T '/path/to/source/file.ext'"

Thank you Stefan.

I don’t fully understand the shell script.
(this is where my inexperience raises its ugly head…again : )

I know that i need to enter my specifics into that script but i am not sure if i have it right

sorry for the disection but

do shell script “/usr/bin/curl ‘ftp://myServer.com/path/to/destination/file.ext’ -u user:pass -T ‘/path/to/source/file.ext’”

user = my user name

bin = ?

curl = ?

ftp://myServer.com/path/to/destination/file.ext’ = the server and the path to folder I am uploading to

-u user:pass-T = ? (i assume i should enter a password for the server? if so, do i completely replacce -u user:pass-T or just a portion…i dont know how to apply this information)

‘/path/to/source/file.ext’" = where the files are being uploaded from ex: a path to a folder on my desktop

here is a sample code to upload files with curl, using your repeat loop
replace the literal strings in the property lines with the URL to the directory on the server, username and password


property ftpserverPath : "[url=ftp://ftp.myServer.com/path/to/directory/]ftp.myServer.com/path/to/directory/[/url]"
property user_name : "myUserName"
property pass_word : "¢¢¢¢¢¢"

repeat with this_item in added_items
	set fileName to name of (info for this_item)
	do shell script "/usr/bin/curl " & quoted form of (ftpserverPath & fileName) & " -u " & user_name & ":" & pass_word & " -T " & quoted form of POSIX path of this_item
end repeat

I was able to fillin the missing information and it did compile.

The bad news is that it doesnt like me calling out the property or my guess is that it doesnt like where i call out the property

so for example:

if the button returned of the result is "OK" then

property ftpserverPath : “ftp://xxxxx/xxxx/xx/x
property user_name : “yyyy”
property pass_word : “zzzzz”

repeat with this_item in added_items
set fileName to name of (info for this_item)
do shell script "/usr/bin/curl " & quoted form of (ftpserverPath & fileName) & " -u " & user_name & “:” & pass_word & " -T " & quoted form of POSIX path of this_item
end repeat
end adding folder items to

my “then” statement must be incorrect or its looking for a tell or something that says “set these properties to this and then do this using those properties”

i see how it should work…but dont know how to make it work…lol

thank you and everyone for their patience…i know is 101 stuff

properties are global settings. Put them at the very beginning of your script before the event handler

PS: the server path must end with a slash

something still isn’t right…the file still doesnt up load…
is it my: if “ok” then


property ftpserverPath : "ftp://xxx.xxx.xxx.x/aaa/bbb/cc/"
property user_name : "zzzzzz"
property pass_word : "yyyyy"

on adding folder items to this_folder after receiving added_items
	set searchString to "LCLPGAD" -- Caching it to a variable for ease of use
	set testDoc to added_items as Unicode text as alias
	
	set oldTID to AppleScript's text item delimiters -- Cache the old delimiters
	set bodyContent to (read testDoc) as Unicode text -- Reading the textfile
	set wordCount to count words of bodyContent -- Count the number of words in the doc (actaully stored in the variable bodyContent)
	set AppleScript's text item delimiters to searchString -- Here's what we're looking for
	set bodyContent to (text items of bodyContent) -- Get the text items from the doc but ignore our search string. This returns a list of text items
	set AppleScript's text item delimiters to " " -- Replace the search string with spaces so we don't end up with one long word
	set bodyContent to bodyContent as Unicode text -- Put the words back together
	set wordCount2 to count words of bodyContent -- Count the words again
	set AppleScript's text item delimiters to oldTID -- Reset the delimiters
	set foundWordCount to wordCount - wordCount2 -- Determine the difference between the two counts and there's your number of occurrences.
	display dialog searchString & " found " & foundWordCount & " times" -- And it does indeed report 6 occurrences.
	if the button returned of the result is "OK" then
		repeat with this_item in added_items
			set fileName to name of (info for this_item)
			do shell script "/usr/bin/curl " & quoted form of (ftpserverPath & fileName) & " -u " & user_name & ":" & pass_word & " -T " & quoted form of POSIX path of this_item
		end repeat
		
	end if
	
end adding folder items to

i tried the upload/shell script on its own…as a folder action script… and it did compile and appeared to do something but it did not upload the file…i added a port to the ftp path (ie ftp://xxx.xxx.xxx.x:21/aaa/bbb/cc/) and it didn’t make a difference

Does your upload work “manually” with Cyberduck or another FTP-client?
If yes, it must also work with cURL, just take the parameters of the client

Hi Stefan - yes, i checked my path in cyber duck and i was able upload via duck.

The url for the duck bookmark reads ftp://123.456.789.9:21/ppp/xxxxxx/bb

and that is what i have in the script (bb followed by /")

i;ve tried it with the port (:21) and without…no chnage

passwords are correct

as i said previously…i tried the script on its own (just to see if there wasnt a problem with another part of the script and it still didnt work

does it have anything to do with source of the orginal file ie POSI-X?

i am clueless (well, thats obvious…lol)

No, POSIX path is necessary, shell scripts work always with POSIX paths.

then I recommend to use a scriptable FTP-client. I have no experiences with it, I always use curl

Thank you for all your help. : )

Is it possibly a punctuation error?? a issue or am i missing a quote??

I took the snippet from a working curl subroutine


.
do shell script "/usr/bin/curl " & quoted form of dest & " -u " & user_name & ":" & pass_word & " -T " & quoted form of source
.

source and dest are POSIX paths

Do you get any error message?
Try it without the folder action, because folder actions aborts silently, if any error occurs

i tried the following script and i get no results - no errors, no uploads

there has to be an easy explanation



property ftpserverPath : "ftp://123.456.789.9/abc/defhij/kl/"
property user_name : "xxx"
property pass_word : "yyy"

on adding folder items to this_folder after receiving added_items
	repeat with this_item in added_items
		set fileName to name of (info for this_item)
		do shell script "/usr/bin/curl " & quoted form of (ftpserverPath & fileName) & " -u " & user_name & ":" & pass_word & " -T " & quoted form of POSIX path of this_item
	end repeat
end adding folder items to


as far as using a scriptable FTP applicaton, i had the script working with Yummy FTP - but Yummy has issues (bombs after upload)

  • Yummy was easy - just call out the bookmark

i have the Cyber duck but havnt figured out the scripting

i am taking another look at that ip address

as mentioned above, you will never get an error message in a folder action
try something like this


property ftpserverPath : "ftp://123.456.789.9/abc/defhij/kl/"
property user_name : "xxx"
property pass_word : "yyy"

-- on adding folder items to this_folder after receiving added_items
set added_items to choose file with multiple selections allowed
repeat with this_item in added_items
	set fileName to name of (info for this_item)
	do shell script "/usr/bin/curl " & quoted form of (ftpserverPath & fileName) & " -u " & user_name & ":" & pass_word & " -T " & quoted form of POSIX path of this_item
end repeat
-- end adding folder items to

just as quick foot note…i tried disecting the script in terminal

results:

  1. i received an access denied error going after the initial IP address (ex: ftp://123.456.789.9/x) as soon as i hit the x it gives me access denied
    user name:pass word are correct and in place

  2. using the entire shell in terminal - with defined paths and specific referance file - i get the error
    curl: (9) uploaded unaligned file size (0 out of 356 bytes)

i will give you last post a try after lunch…

thank you again

update: tried the latest script with no success

GOT IT!!! : ))

The error was in ftpserverPATH! there was a missing / after the ftp address! it should read ftp://123.456.789.9//xxx/xxx/x/

Stefan - thank you very much for your help!! Now I can get back to being a graphic artist. Thank you again for all your patience and insight.

I’ve learned a great deal from this experience

Thank you!

Thank you!!

Thank you!!!

Rick