Hi guys, i have a script that obtains a txt file from the system profile and saves it to a network share.
Each file is named with the computers machine name .profile.txt
what i would like to do is search the network folder to see if a script from that machine is currently there, if it is, exit the script, if its not, run the script and check again on completion.
is this possible?
the purpose of this script is for admin/assest reasons so we have a log of the machine details.
heres my script as it stands;
try
-- Mounts the share and authenticates, once mounted, a link is created in a hidden folder called /Volume
tell application "Finder"
mount volume "smb://user:password@1.0.0.1/share"
end tell
-- Obtains the computers name and saves it in a variable called hostname
set hostname to do shell script "grep -A1 'ComputerName<' /Library/Preferences/SystemConfiguration/preferences.plist | grep -v key | sed s/\"[[:blank:]]*<\\/*string>\"//g"
-- Runs system profiler and saves it to a file as 'computername'.profile.txt to the share
do shell script "/usr/sbin/system_profiler > /Volumes/share/" & quoted form of hostname & ".profile.txt"
-- Unmounts ALL drives that arnt in use except the root, only root & share should be mounted
do shell script "umount -A"
on error
end try
Cheers, Dave
Dave,
I’m not exactly sure what you’re trying to do here, but here is code to see if somthing exists.
if exists file "scriptName.scpt" of disk "whateverDriveName"
You’ll have to specify exactly where on that disk the script should be (e.g. a folder in another folder on disk whatever) or have to write more code to search the entire disk (which, with my limited knowledge, would involve repeats and be quite slow).
Here is where I’m not sure what script you want to run; the code you have so far or the script you’re looking for from another machine. I’m assuming this code in which case you put:
on idle
-- your code
return 30 -- how many seconds to wait before checking again
end idle
Don’t know if this helps or not.
PreTech
What i want to do is search the network share to see if the script has already been run and has saved a txt file with the computers name in it, “ibook.profile.txt”. in the code ive posted above i use a variable called hostname to store the computer name in.
Ive tryed the code you surjested to see if the file exists but couldnt get it to work using the variable in the file name;
if exists file "& quoted form of hostname & ".profile.txt"" then
...
Also, is there a way of searching the complete share for files with spaces at the end of them, eg: "filename "
Cheers
Dave,
If the variable “hostname” is just the name of the computer as text, then you can set the name of your file to search for like so:
set fileName to hostname & "." & "profile.txt"
and then search the disk with:
tell application "Finder"
if exists file fileName of disk diskName then
--do your thing
end if
end tell
As far as searching the share this is a way. I tried to use a “set allFiles to every file of someFolder whose name’s last character is ’ '” but that doesn’t quite work. I’m sure somebody has a good one liner. However, even if the extension of the file is hidden, it still has a .txt, .scpt, & so on after it. This will work but will take time depending on how many files need to be searched. Currently this is set up for you to choose a folder to search. If there are a number of folders inside that folder then a recursion will need to be used. I can post that for you or you can search for this. I believe I recently posted something similar for an Illustrator script searching layers and sublayers. The principle is the same. Anyway, here 'tis.
set x to choose folder
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
tell application "Finder"
set t to name of every file of x
repeat with aName in t
set f to first text item of aName -- this is assuming a name such as testfile.txt. If your name is like hostname.filename.ext, then you will need to specify the second, third or so on text item for the actual file name.
if last character of f is " " then display dialog "y"
end repeat
end tell
set AppleScript's text item delimiters to tid
Hope this helps.
PreTech
Almost there!!
that works if the file name is there, but if it isnt the script still runs and produces a system profiler report!!
how would i go about ending the script if the fileName ISNT on the share?
ive tryed some else and else if commands but none seem to do the trick?!?!
Dave,
You could do your test like this:
if not exists file fileName of disk diskName then
quit me
end
I believe this will work. Your script would have to be saved as an application though. If you use this code in script editor it will make the editor quit.
PreTech
Still doesnt work =(
Heres my code as it stands;
-- Mounts the share and authenticates, Once the Share is mounted, a link is created in a hidden folder called /Volume
tell application "Finder"
mount volume "smb://username:password@194.0.0.1/share"
end tell
-- Obtains the computers name and saves it in a variable called hostname
set hostname to do shell script "grep -A1 'ComputerName<' /Library/Preferences/SystemConfiguration/preferences.plist | grep -v key | sed s/\"[[:blank:]]*<\\/*string>\"//g"
set fileName to hostname & "." & "profile.txt"
set diskName to "/Volumes/share/"
tell application "Finder"
if file fileName of disk diskName exists then
-- Unmounts ALL drives that arn't in use except the root, only root & Share should be mounted
do shell script "umount -A"
quit me
else
try
-- Runs system profiler and saves it to a file as 'computername'.profile.txt to the Share
do shell script "/usr/sbin/system_profiler > /Volumes/share/
& quoted form of hostname & ".profile.txt"
-- Unmounts ALL drives that arn't in use except the root, only root & Share should be mounted
do shell script "umount -A"
on error
end try
end if
end tell
Any Ideas???
Dave,
Does your disk name actually contain the /'s? The Finder (as far as I know) would show the name something like “MacIntosh HD”. Also is the “share” part of the disk name actually part of the disk name or is it a folder on that disk?
I’m assuming that the disk name is “Volume” and a folder named “share”. So that would be:
set locationToSearch to "Volume:share:"
if exists file fileName of folder locationToSearch then
-- Unmounts ALL drives that arn't in use except the root, only root & Share should be mounted
do shell script "umount -A"
quit me
else
try
-- Runs system profiler and saves it to a file as 'computername'.profile.txt to the Share
do shell script "/usr/sbin/system_profiler > /Volumes/share/
& quoted form of hostname & ".profile.txt"
-- Unmounts ALL drives that arn't in use except the root, only root & Share should be mounted
do shell script "umount -A"
on error
end try
end if
This is where I think your problem is, however, I can’t test this on my computer. If this doesn’t work, let me know where the error is occuring. Try commenting out the ‘quit me’ and run as a script with the eventlog clicked and see where the error is happening.
PreTech
That worked like a charm!! 
thanks for all the help!!
dave