I have a desktop application that writes an XML file out. If the XML file is open in a text editor (from a prior running of the script), when Livecode saves the file out, the XML data is not changed.
The actual line I'm using to write the file:
put tOutputData into URL ("file:" & tFilePath)
I can see that using "open file-->write-->close file" logic instead of "URL (file:)" will over-write the data if the output file is already open in a text editor. However, the text editor will show the "old" data until the file is reloaded.
How do I ask the file system if the file is already in use before attempting to write to the file in Livecode?
Check the result to see if there was a problem accessing the file:
put tOutputData into URL ("file:" & tFilePath)
put the result into rslt
if rslt is not empty then
beep
answer error rslt
end if
Related
I'd like to have a script that I can run where it basically takes the .rdp file that I have selected in finder, and append a line of text to the end of it.
e.g
I download a .rdp file to use in Microsoft Remote Access and to speed up my workflow I'd like to append the text 'Use Multimon:i:1' at the end before I launch it so that I don't have to open the preferences each time.
I'm not too familiar with AppleScript so would appreciate any advice on how to achieve this.
Thanks!
It is my understanding that .RDP files are saved in plain text format. If this is the case, using the do shell script command in an AppleScript, appending text to a file is fairly simple. This following AppleScript code should work for you.
Paste this following code into a new Script Editor.app document. Then
with your .RDP file currently selected in Finder, run the code in Script Editor.app and it will append the text to your file.
property addText : "Use Multimon:i:1"
tell application "Finder" to set selectedFile to POSIX path of ((get selection) as alias)
do shell script "echo " & quoted form of addText & " >> " & quoted form of selectedFile
This currently works for a selected file with the extension 'txt'. Test it and if it works for you, edit it to your desired extension.
The 'if' statements are to ensure that you don't accidentally append the text to a binary file which could corrupt that file. The 'return' means that your text will appear on its own line. If you don't want that, remove 'return & '. For details on 'open for access', please see the Language Guide: Commands Reference. On the same page, you can find 'close access'.
tell application "Finder"
set tFile to selection as alias
if name extension of tFile is "txt" then
set corR to true
else
display alert "Are you sure you've selected the correct file?"
set corR to false
end if
end tell
if corR is true then
set ab to open for access tFile with write permission
write return & "Use Multimon:i:1" to ab starting at eof
close access ab
end if
I am exploring PDF file format and trying to edit and manipulate its internal data. the problem is that I noticed I always get corrupted files after making any minor change to a file so I tried a very simple example to just read the pdf data and rewrite it to a new "file.pdf" without making any changes, as follows:
file = open('sample.pdf','r',encoding='ansi').read()
file_ = open('output.pdf','w').write(file)
but again I got a corrupted file (can't be opened using Adode reader) so I tried to open it using Google Chrome and it worked properly but with the font has changed to the default instead of the original font file.
I opened the input and output files and compared them using notebad++ and two files matched exactly!
I also opened the output file and copied its content and pasted it to the input file and surprisingly, it worked well, exactly as the input file.
Any ideas what is the problem?
I am trying to make a game in python 3.6 that saves data using a text file.
text = open("text_file.txt", "a")
text.write("Line 1")
text.write("Line 2")
text.write("Line 3")
text.close()
text = open("text_file.txt", "r")
print(text.read())
text.close()
Is there an easier way to do this?
I know about 'r+', but it is a combination of read and write. The problem with that is that the write part of it resets the text document back to blank when it opens the file.
From the fopen(3) man page:
a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. The initial file position
for reading is at the beginning of the file, but output is
always appended to the end of the file.
My dilemma is that there is pdf file named 'P01111-AMFLIBL.pdf' , I want to append '.NOSUFFIX' after .pdf so the result will be like 'P01111-AMFLIBL.pdf.NOSUFFIX'.
I am trying to add by this line of code :
fileName = fileName + ".pdf" + ".NOSUFFIX";
It's working, but the problem is that pdf file is not directly getting opened into my system
On Windows, the extension (i.e. suffix starting at last .) determines the file type (and associated program). If you append another suffix, this appended .NOSUFFIX will become the file extension, and the original .pdf will become just a part of the file name.
I have an iCal (Calendar) alarm that is set to go off everyday. It parses a csv that contains emails and a date, then sends an email if it's the appropriate day. The csv is statically coded because I want it to run in the background.
Here is the code for reading the file:
set theLines to paragraphs of (read (open for access file "csv path"))
The problem I'm encountering is that if I modify the csv file (add or remove lines), it seems to read it incorrectly. From what I can tell, prior to modifying it, it works fine.
After I modify it (which I must do periodically), it doesn't read it correctly because it's basing it off of a cached version or something. My only thought is that I'm reading the file incorrectly. Is there a more appropriate way to read a file? I'm new to AppleScript, but not to programming.
You don't need to "open for access". Try this:
set theLines to paragraphs of (read "/Users/Grant/path/to/your/file.txt")
or
set theLines to paragraphs of (do shell script "cat '/Users/Grant/path/to/your/file.txt'")