Hi all, I am new to applescript and still learning.
I have a simple two line text file that i would like to populate with the computer’s hostname at the end of the file. Can someone help me with this please?
Ideally, i would need to check line 2 of the text file to see if it starts with “id=”. If it does, display line 2 (or the value after “=”) in a text box to be able to edit it if needed.
If there is no line 2 or nothing after the prefix, then fill in the prefix and auto-fill the hostname of the computer. Then display line 2 (or the value after “=”) in the text box to verify that the entry is correct.
once a user clicks ok, it will save the file and close.
set HostName to (do shell script "hostname -s") -- -s removes the domain, usually .local
display dialog HostName
You don’t tell us enough about the file you want to alter; it’s path for example – or do you want to choose it? The usual process would be to open the file for access with write permission, read it in as a variable in your script, parse it however you wish, i.e. look for id= in paragraph 2, then write the amended text back to the file and close access. Something like this:
set tFile to (choose file)
try
set F to open for access tFile with write permission
set fileText to read F
set P1 to paragraph 1 of fileText
set HostName to (do shell script "hostname -s")
display dialog HostName
if (count paragraphs of fileText) = 1 then -- second paragraph missing
set P2 to "id=" & HostName
else -- there is a second paragraph
set P2 to paragraph 2 of fileText
if P2 contains "id=" then
set P2 to P2 & HostName -- add the hostname
else
set P2 to "id=" & HostName -- add the whole thing
end if
end if
set eof of F to 0 -- erases the original file
write P1 & return & P2 to F -- write the new text
close access F -- close the file
on error
close access F -- if all hell breaks loose, close the file.
end try
Bear in mind that this script will lose anything else in the file after paragraph 2 but you say it’s a two-line file.
Thank you so much for the kick in the right direction, I really appreciate it!
I was able to build on what you suggested and ended up with an applescript that accounts for a couple of different configurations, and I am still expanding.
set tFile to ("some:file:here")
try
set F to open for access tFile with write permission
set fileText to read F
set P1 to paragraph 1 of fileText
set P2 to paragraph 2 of fileText
set hostname to (do shell script "hostname -s")
display dialog "What should the id be set to?" with icon note buttons {"blank", "custom", "hostname"} default button 3
if button returned of result is "blank" then
set P2 to "" -- Line 2 should be blank
else if button returned of result is "custom" then
display dialog "What should it be set to?" with icon note buttons {"cancel", "Ok"} default answer "" default button 2
set P2 to "id=" & (text returned of result) -- Line 2 should be set to the result of the text box
else if button returned of result is "hostname" then
set P2 to "id=" & hostname -- Line 2 should be set to the hostname of the computer
end if
display dialog "The properties have been changed to:" & return & P1 & return & P2 buttons {"cancel", "ok"}
set eof of F to 0 -- erases the file
write P1 & return & P2 to F
close access F
on error
close access F
end try