AppleScript Problem

All of a sudden, I can't create a text file that generates on the desktop. Also I can't write text to a text file anymore using a `do shell script` or `write text to textFile`. New documents are affected but old scripts that I wrote a couple weeks ago write to their already generated desktop text files. I don't know what happened. I tried restarting. Could it be Big Sur?

MacBook Pro 13″, macOS 11.2

Posted on Mar 19, 2021 2:23 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 20, 2021 2:30 PM

Unless I'm missing something, your script should never have worked.


Your script defines:


set textFile to (("Macintosh HD:Users:clusterflux:Documents:") & "DOG.txt")


so the variable textFile is a text object. Not a file.


Therefore the subsequent line:


	write newText1 to textFile


will fail 100% of the time. Theoretically, regardless of what OS version you're running. You can't write a value to a text variable. You need to coerce textFile to a file object first.


Additionally, you should be using open for access to get a file handle to the file, and directing your writes to that.


set textFilePath to (("Macintosh HD:Users:clusterflux:Documents:") & "DOG.txt")
set textFile to open for access textFilePath with write permission
...
-- write data to the file
write someData to textFile
...
-- and close the file when you're done
close access textFile


Writing to it via a shell script is also doomed to fail:


	do shell script "echo  " & quoted form of newText2 & " >>  " & quoted form of textFile


because shell commands require POSIX paths, not MacOS (colon-delimited) paths.

Similar questions

15 replies
Question marked as Top-ranking reply

Mar 20, 2021 2:30 PM in response to macandcheesespaghetti

Unless I'm missing something, your script should never have worked.


Your script defines:


set textFile to (("Macintosh HD:Users:clusterflux:Documents:") & "DOG.txt")


so the variable textFile is a text object. Not a file.


Therefore the subsequent line:


	write newText1 to textFile


will fail 100% of the time. Theoretically, regardless of what OS version you're running. You can't write a value to a text variable. You need to coerce textFile to a file object first.


Additionally, you should be using open for access to get a file handle to the file, and directing your writes to that.


set textFilePath to (("Macintosh HD:Users:clusterflux:Documents:") & "DOG.txt")
set textFile to open for access textFilePath with write permission
...
-- write data to the file
write someData to textFile
...
-- and close the file when you're done
close access textFile


Writing to it via a shell script is also doomed to fail:


	do shell script "echo  " & quoted form of newText2 & " >>  " & quoted form of textFile


because shell commands require POSIX paths, not MacOS (colon-delimited) paths.

Mar 20, 2021 5:38 PM in response to macandcheesespaghetti

> From the top of my head though, I ran into problems setting open for access because it returned some sort of error that I can't recall right now but from what you wrote I didn't call it correctly


I will concur that file handling within AppleScript is a bit of a mess and even experienced AppleScripters trip up and forget exactly which form of file/path/alias to use, and which commands will automatically coerce one to another..


The other thing to learn about is the path to command which will find a number of common locations at runtime (i.e.. without you having to know the full path to the current user's home directory, for example).

A number of seemingly-valid paths can trip up due to typos, or some minor difference between systems and path to can avoid a lot of them.


In general, here is the robust logic for writing to a file on the desktop that should work for any system:


-- define the file path using 'path to desktop' to find the dynamic/current user desktop folder
set myFilePath to (path to desktop as text) & "my.txt"

-- open the file (this will create it if needed)
set myFile to open for access myFilePath with write permission

-- optionally reset the file contents (if any)
set eof myFile to 0

-- write some data to the file
write "blah " & return to myFile
-- you can have multiple write statements
write "more data here" & return to myFile

-- close the file when done
close access myFile


Just make sure you open for access before you reference the file, and close access at the end of your script.

Mar 19, 2021 9:37 AM in response to macandcheesespaghetti

Lots of information missing here.


When you say you "can't create a text file...", what do you mean?


Do you mean the script runs, but no file is created?

Do you mean the script throws an error? What is that error message?

Are you able to create files in other locations, or is it just the Desktop that's affected?


and how are you running the script? as a script app? from within Script Editor?

What about new scripts? are they able to create files? Can you recompile/save the old scripts and make them work?


The answers to these questions may help identify where the problem is. It is entirely likely that it's related to new security protocols, but that's not to say it can't be done at all.

Mar 19, 2021 7:58 AM in response to macandcheesespaghetti

It wouldn't surprise me at all if it was Big Sur. I haven't used it but when I had problems with text files and Applescripts after upgrading to a higher OS I discovered Apple's ever-increasing security messages prevented old scripts from doing things with text files. I can't really help on the issue though, sorry. Those were transitions from OS 10.4 to OS 10.9 and I am sure things have gotten a lot worse with the security features of the past 6 releases.

Mar 20, 2021 4:56 PM in response to macandcheesespaghetti

Don't want to be negative. We were in your position before. I forget things about applescript all the time. When writing computer code precision is important in the code and in the question.


All of a sudden, I can't create a text file that generates on the desktop.

This statement confused me. I assumed the same code was working before and it is not now. I should not have assume: *** of u & me.


Writing to files in applescript have been nightmare for me. Figure out out which reference to use to access a file has been an endless hassle for me: classic macos, modern macOS and Unix


Good luck with your new adventure.


These days, I put a "print" line about after every line of code.



(* 


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.




	Author: rccharles
	
	For testing, run in the Script Editor.
	  1) Click on the Event Log tab to see the output from the log statement
	  2) Click on Run


 *)




on run
	-- Write a message into the event log.
	log "  --- Starting on " & ((current date) as string) & " --- "
	--  debug lines
	set desktopPath to (path to desktop) as string
	log "desktopPath = " & desktopPath
end run





Robert



Mar 19, 2021 5:41 PM in response to Camelot

I run the file with either

`write text to textFile`

or

`do shell script "echo "  & quoted form of text & " >>  " & quoted form of textFile`

and it returns an error `do shell script` returns: sh: Macintosh HD:Users:clusterflux:Desktop:DOG.txt: Read-only file system. `write...` returns: Can’t make "Macintosh HD:Users:clusterflux:Documents:DOG.txt" into type file. I've also noticed that

`set textFile to (("Macintosh HD:Users:whomever:Documents:") & "TEST.txt")`

doesn't create a text file in the path location anymore when it used to. I can't create a file to desktop or documents, I haven't tried other locations. I'm running the script from script editor by clicking the play button.


Old scripts that I wrote several weeks ago still write to a file but I don't want to mess with them because they work. I tried disabling SIP (start in recovery and type `csrutil disable` in terminal) but that didn't fix it.

Mar 19, 2021 9:14 PM in response to Limnos

I seem to remember in the day, you had to turn on assistive device setting or some-such-setting to get automation to work in applescript. It was something along the lines to let applescript to send keystrokes to the apple applications. I have no proof, but encryption is for honest people I feel.


Now, apple send boxes all apps. You need to give them permission to *art. I got enough problems surfing the web to add to my headaches with more security.


I learned a long time ago the only secure computer is in a locked room. I follow this policy. It works for me. My harddrive failed for me in the late 90's and the backup floppy with all my passwords failed too. It was encrypted, so I could not recover the good sectors. I changed my strategy to kiss with multiple backups. It's a risk.


R

Mar 20, 2021 3:38 AM in response to rccharles

I don't mind posting the whole code. Here:


set ListOfVariables to {}

set VariableBlock to paragraphs of (read file "Macintosh HD:Users:clusterflux:Desktop:Test.txt")

repeat with nextLine in VariableBlock

if length of nextLine is greater than 0 then

copy nextLine to the end of ListOfVariables

end if

end repeat


set textFile to (("Macintosh HD:Users:clusterflux:Documents:") & "DOG.txt")

set PropertyVariables to "property eGrepœ1 :  µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{2},ΩΩd{3}.ΩΩd{2}µ

property eGrepœ2 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{2},ΩΩd{3}.ΩΩd{2}µ

property eGrepœ3 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{1},ΩΩd{3}.ΩΩd{2}µ

property eGrepœ4 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{3}.ΩΩd{2}µ

property eGrepœ5 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{2}.ΩΩd{2}µ

property eGrepœ6 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{1}.ΩΩd{2}µ

property eGrepœ7 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{1}.ΩΩd{1}µ

property eGrepœ8 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{1}.ΩΩd{3}µ

property eGrepœ9 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{1}.ΩΩd{4}µ

property eGrepœ10 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{1}.ΩΩd{5}µ

property eGrepœ11 : µpriceValue___11gHJΩµ>ΩΩ$ΩΩd{1}.ΩΩd{6}µ

"

set BlankPropertyVariables to "property current®Price : missing value

"

repeat with theItem in ListOfVariables

set findText1 to "œ"

set replaceText1 to theItem

set newText1 to do shell script "sed 's|" & quoted form of findText1 & ¬

"|" & quoted form of replaceText1 & "|g' <<< " & quoted form of PropertyVariables

write newText1 to textFile

(*do shell script "echo  " & quoted form of newText1 & " >>  " & quoted form of textFile*)

end repeat


repeat with theItem in ListOfVariables

set findText2 to "®"

set replaceText2 to theItem

set newText2 to do shell script "sed 's|" & quoted form of findText2 & ¬

"|" & quoted form of replaceText2 & "|g' <<< " & quoted form of BlankPropertyVariables

write newText2 to textFile

(*do shell script "echo  " & quoted form of newText2 & " >>  " & quoted form of textFile*)

end repeat


set IdleLoop1 to "on idle

getPrices()

set {year:y, month:m, day:d} to (current date)

tell application µMicrosoft Excelµ

open µ/Users/clusterflux/Desktop/人MASTER人_excel.xlsxµ

set display alerts to false

first row index of (get end (last cell of column 2) direction toward the top)

set LastRowCrypto to first row index of (get end (last cell of column 2) direction toward the top)

set value of cell (µBµ & LastRowCrypto + 1) to y

set value of cell (µCµ & LastRowCrypto + 1) to (µ=TODAY()µ)

set value of cell (µDµ & LastRowCrypto + 1) to d

set value of cell (µEµ & LastRowCrypto + 1) to (time string of (current date))"

write IdleLoop1 to textFile

(*do shell script "echo  " & quoted form of IdleLoop1 & " >>  " & quoted form of textFile*)


the part that errors out is:

write IdleLoop1 to textFile

and

do shell script "echo  " & quoted form of IdleLoop1 & " >>  " & quoted form of textFile


Mar 20, 2021 3:51 AM in response to rccharles

I don't really put much stock in the security provided to consumers. I encrypt but I assume they can just brute force that if they really have the time and interest. I just assume there is no private place in cyberspace. Some people believe you can can subvert but the security of anonymous and secure surfing always seems to end up compromised by *somebody*. I'm not really interested in much that requires much security, though I did have a job where I cared whether the files I made were compromised by intellectual copyright thieves. I don't really feel secure enough with consumer technology to have a high security job from home. I just didn't learn a lot of that from my. education. Just how to create secure passwords and save them on paper, but like I said, passwords can be breached by somebody with time and resources.

Mar 20, 2021 3:23 PM in response to Camelot

I'm a complete noob so I appreciate the notes. Just working on a hobby freelance so the educational resources are kind of unreliable (that's why I posted the whole script because I assumed it was something I was writing wrong). I'm going to invest in a book soon but I'm trying to save money. Thanks for looking out for something you can help on. From the top of my head though, I ran into problems setting open for access because it returned some sort of error that I can't recall right now but from what you wrote I didn't call it correctly (just learned about it from a comment with no example of how to use it). The script I provided in the response worked for some time with the do shell script but I will try with POSIX paths although I pretty sure I tried that to and it still returned an error. Just not experienced yet in the different tricks for certain commands, just learning from the resources available on forums for questions that don't fit my parameters very well.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript Problem

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.