Does anyone know if there any limitations around what a folder action can do without going down the Perl/shell scripting route?
I am trying to open a dropped text file, and replace the carriage returns with commas.
My stand alone script works OK but I want to make it an invisible process.
Running Under Pather.
Also is there a smart way of doing it? The current process is as follows:
choose text file
read contents to a vaiable as list using return as delim
pick first 7 chars(return is 8th)
replace in list
rewrite contents back to file
Folder Actions are what you see: fire applescript statements under certain conditions. And I don’t think that Perl/whatever can actually do this.
If you are talking about AppleScript limitations handling text… Hmmm… You can use “do shell script” and work with perl or whatever you like (php, shell, python, etc.). For a simple search/replace, I would just use plain AS TIDs and the read/write commands, which are pretty fast.
You can edit the “plst” resource of your applescript-app (or “plist” file, if bundle) and use the following tag:
Just re-reading your post, when I try to remove the unwanted returns from the data it does not replace them.
property type_list : {"text"}
tell application "Finder"
activate
set these_items to choose file
set the item_info to the info for these_items
set item_name to the name of this_info
set file_to_read to item_name
set pos_data to my read_this_file(file_to_read)
repeat with i from 1 to the count of the items in the pos_data
try
set test_name to item i of the pos_data
if test_name starts with "U" then
copy "" to (item i of the pos_data)
else
set AppleScript's text item delimiters to "" set new_name to (characters 4 thru 10 of test_name) as string
copy new_name to (item i of the pos_data)
end if
end try
end repeat
set AppleScript's text item delimiters to ","
set file_content to pos_data as string
set write_path to ((this_item as string)) as alias
set the write_path to the write_path as text
set the open_target_file to open for access the file write_path with write permission
set eof of the open_target_file to 0
write file_content to the open_target_file starting at eof
close access the open_target_file
beep 2
end tell
on read_this_file(file_to_read)
try
set file_to_read to file_to_read as text
set the retrieved_info to read alias (file_to_read) using delimiter return as list
return retrieved_info
on error
beep 1
return 0
end try
end read_this_file
Hi!
I don’t understand very well what are you doing with this code, but the basics of search/replace using TIDs is the following:
set inputString to "some text"
set AppleScript's text item delimiters to "text" --> enter search string
set inputString to inputString's text items --> {"some ", ""}
set AppleScript's text item delimiters to "stuff" --> enter replace string
set inputString to inputString as text --> coerce list back to string using replace string
set AppleScript's text item delimiters to {""} --> reset TIDs to its default value
inputString --> "some stuff"
Adding a “LSUIElement” tag to the plist of an app should make it a background-faceless app. But just forgot it, since a folder action is already a background process :oops:
This all works fine if you can define a visible cahracter.
How can I do it with return as the delimiter?
AS seems to ignore this!!
This is a simple test version (based on lots of bits you have helped me with!!)
tell application "Finder"
set my_file to choose file
set my_data to read my_file using delimiter return
display dialog my_data as string
set AppleScript's text item delimiters to return
set new_list to the text items of my_data
display dialog new_list as string
set AppleScript's text item delimiters to ""
end tell
When you read using delimiters you already pick a list:
read alias "path:to:file.txt" using delimiter return
--> {"paragraph 1", "paragraph 2"}
Now let’s imagine that you wish an output list where you will replace any coincidence of “paragraph” with “something” → {“something 1”, “something 2”}
set listOfParagraphs to (read file "path:to:file.txt" using delimiter return)
--> {"paragraph 1", "paragraph 2"}
set finalList to {}
repeat with thisParagraph in listOfParagraphs
set end of finalList to my searchReplace(thisParagraph, "paragraph", "something")
end repeat
finalList
--> {"something 1", "something 2"}
to searchReplace(thisText, searchTerm, replaceTerm)
set AppleScript's text item delimiters to searchTerm
set thisText to thisText's text items
set AppleScript's text item delimiters to replaceTerm
set thisText to thisText as string
set AppleScript's text item delimiters to {""}
return thisText
end searchReplace
You can include your own criteria in the repeat loop, such as:
if thisparagraph starts with "U" then set end of finalList to my searchReplace(thisParagraph, "paragraph", "something")
(which will strip any paragraph starting with “U” from the “finalList”.