Hi guys,
I have and number of AppleScripts running on network machines and I am trying to make them write data in one single file and the data get read by an Applescriptobjc app running on a server, I tried to write to text file but for some reason, the scripts could not write to the file on the network, so my other option is to write to an SCPT file on the network (I have not tried yet), for now it seem complicated for ASOC app to read from this file, and it gives me all kind of errors when trying to put the file inside the app, so my questions are:
1- is there a was for ASOC to read SCPT files
2- is there a better way to make AppleScripts app and ASOC communicate well?
I am having a problem writing text files to the network, it works fine on local computer, but it seem to have a problem with permissions even though I have full access to it
any idea?
here is my code, I am testing it now at home and it works fine, but at work it dos not, also I should mention I have an ASOC app that writes to a text file on the network and it works fine, but this one don’t
property logFilePath : "/Volumes/home/admin/NAS/temp/MACHINESLOG.txt"
my updateLog("GFX1", "Desktop Cleaned")
on updateLog(PCName, task)
set dd to getToday()
set logline to PCName & " " & task & " " & dd
try
set thePath to logFilePath
set thePath to POSIX path of thePath
set textFile to (open for access POSIX file thePath with write permission)
get eof textFile
set oldText to read textFile
set eof textFile to 0
write logline & return & oldText to textFile -- I reverted this so it write the new line on the top
close access textFile
end try
end updateLog
on getToday() -- here I construct todays date
set dw to weekday of (current date)
set dd to day of (current date) -- get the day
if dd < 10 then
set dd to "0" & dd as string -- add zero if the number is less than 10
end if
set mm to month of (current date) -- get month
if mm < 10 then
set mm to "0" & mm * 1 as string -- *1 is to transform the word of the month to number
else
set mm to mm * 1
end if
set yy to year of (current date) -- get the year
set tt to time string of (current date)
set numiricDate to dw & " " & dd & "-" & mm & "-" & yy & " " & tt & " " as string
return numiricDate
end getToday
Also, that getToday handler isn’t very attractive. You’re calling current date multiple times, among other things. You’re using ASObjC, so you might as well use the tools designed for this sort of thing:
set theDate to current application's NSDate's |date|()
set df to current application's NSDateFormatter's new()
df's setDateFormat:"EEEE yyyy-MM-dd hh:mm:ss a"
(df's stringFromDate:theDate) as text
What file sharing protocol are you using? A file can only be written once at the same time by the kernel and locally it work perfect. When opening a file over network with write privileges becomes more complex because the virtual file systems needs to ask the server if the file is available for write or not. Different protocols uses their own methods and with smb you can even choose different kind of locking.
Easiest way to do this is writing atomically to the file. Write local file first and when done copy to network volume. So when read and writing to the file you’re actually working with a local copy. You could also use a lock file where you indicate which service and program is currently owns permission to the file so other scripts will wait for the script to be finished.
That means that whatever last opened it with read access hasn’t closed it. You need to make sure you do that – writing atomically as DJ suggested is probably bast. Post your file writing code…