rewrite and append filename to text file

It’s a working script now and I thought I would share it back since I got your help making it work.
http://bbs.applescript.net/viewtopic.php?pid=50844#p50844
i glean from other scripters, so anyone can take what they can use from here.
If there is anything particularly clumsy, please point it out. I’d be happy to learn how to make cleaner work.

This script takes a text file, re-writes it to a temporary file named “importMeNext.txt” and in the re-writing adds the file name to every line of text. The script then triggers Filemaker Pro to run a script of its own to import this file. After a delay (to make sure Filemaker has time to finish its work) the temporary file is erased and the text file is moved to a done folder.
I intend to make it a folder action, and have text files that arrive from a particular source get automatically imported into Filemaker.

Thanks for the help,
JA

set importFolder to "Macintosh HD:Users:jamcrae:Desktop:watched folder:New Manifests:" as alias
set doneFolder to "Macintosh HD:Users:jamcrae:Desktop:watched folder:Imported Manifests:" as alias

set picFolder to importFolder as Unicode text

tell application "Finder"
	try
		set picList to every file of folder picFolder as alias list
	on error number -1700
		set picList to first file of folder picFolder as alias as list
	end try
end tell

repeat with eachFile in picList
	-- picList contains aliases, so need for 'file' before 'eachFile'.
	set myData to (read eachFile as text using delimiter return)
	set howMany to count of items in myData
	set fileName to name of (info for eachFile)
	try
		copy (open for access file "Macintosh HD:Users:jamcrae:Desktop:watched folder:ImportMeNext.txt" with write permission) to fileRef
		repeat with i from 1 to howMany
			write (item i of myData & "," & fileName & return) to fileRef
		end repeat
		close access fileRef
	on error
		close access fileRef
	end try
	
	tell application "FileMaker Pro"
		activate
		do script "Import Script"
	end tell
	delay 15
	
	tell application "Finder"
		activate
		---
		try
			move eachFile to folder doneFolder
		on error
			set name of eachFile to ("1" & fileName)
			move eachFile to folder doneFolder
			
		end try
		try
			set myFile to (open for access file "Macintosh HD:Users:jamcrae:Desktop:watched folder:ImportMeNext.txt" with write permission)
			set eof myFile to 0
			close access myFile
		on error
			close access myFile
		end try
			
	end tell
	
end repeat

Thanks for sharing that, JA. It looks very solid. :slight_smile:

If you’re interested, there’s a function in the StandardAdditions called ‘path to’, which automatically returns the path to certain key locations on the host system. It takes a keyword parameter representing the location and an optional ‘as’ parameter for the form of the result. (Without this optional parameter, it returns an alias.) It would save you some typing in your script and would allow it to work for any Mac OS user without the need to adjust the paths:

set watchedFolder to (path to desktop as Unicode text) & "watched folder:"
--> "Macintosh HD:Users:jamcrae:Desktop:watched folder:" on your machine.
--> "PowerBook HD:Users:nigelgarvey:Desktop:watched folder:" on mine.

set importFolder to (watchedFolder & "New Manifests:") as alias
set doneFolder to (watchedFolder & "Imported Manifests:") as alias

-- Similarly with the file path later on.

It’s quite common to see ‘(path to desktop as Unicode text)’ written as ‘(path to desktop) as Unicode text’. These produce exactly the same results but aren’t quite the same process:

(path to desktop as Unicode text)
-- 'as' is a 'path to' parameter: 'path to' returns the Unicode text.

(path to desktop) as Unicode text
-- 'as' is a language keyword: 'path to' returns an alias, which AppleScript
-- then coerces to Unicode text. Takes very slightly longer.

You’ll probably see ‘as’ formatted in different colours if you compare these in Script Editor.

I hope the giant lightbulb going on over my head didn’t blind you. :wink: This is brilliantly useful. The scripts that I write for the office won’t need to be rewritten for each person’s machine. I won’t need one for my desktop mac and one for my laptop.
I need to look up what the other ‘path to’ destinations are.
Thank you for your feedback. You’ve been invaluable.
JA

Just for the record here, these are the paths to:

application support/applications folder/desktop/desktop pictures folder/documents folder/favorites folder/Folder Action scripts/fonts/help/home folder/internet plugins/keychain folder/library folder/modem scripts/movies folder/music folder/pictures folder/preferences/printer descriptions/public folder/scripting additions/scripts folder/shared documents/shared libraries/sites folder/startup disk/startup items/system folder/system preferences/temporary items/trash/users folder/utilities folder/workflows folder/voices/apple menu/control panels/control strip modules/extensions/launcher items folder/printer drivers/printmonitor/shutdown folder/speakable items/stationery : the folder to return

These are the origins a path can have:

[from system domain/local domain/network domain/user domain/Classic domain] : where to look for the indicated folder

These are the path classes:

[as type class] : the type to return: alias or string (default is alias)

And this puts a folder there or not:

[folder creation boolean] : Create the folder if it doesn’t exist? (default is true)
→ alias : the path to the specified folder