Applescript: "File Already Open" when writing to new file - ios

I'm creating a text file whose file name will consist of constant and variable strings. For whatever reason, I'm getting an error saying "[file name] is already open" when I'm actually just creating it. The file is created, but none of my content makes it into the file.
All of the fixes I've tried end in another error saying "network file permission."
Also, I should mention that my new file is going into the same container as another file that is used to create the new file, which is where the filePathAlias comes in.
Any ideas? Thanks in advance!
Here's the script:
-- get the file --
set filePathAlias to (choose file with prompt "** Choose File **")
set filePath to filePathAlias as string
tell application "Finder"
set fileName to name of filePathAlias
set containerPath to (container of filePathAlias) as string
end tell
set filePath to filePathAlias as string
-- get file container --
tell application "Finder"
set containerPath to (container of filePathAlias) as string
end tell
-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"
-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
write listTextFinal to myFile as text
close access myFile
on error
close access myFile
end try

You didn't give an example path for filePathAlias or locationName. I was unable to reproduce the file already open error. I can reproduce the network file permission error...So:
set filepathalias to ((path to desktop folder as string) & "test" as string) as alias
--alias of folder on desktop called test... massaged well to be an alias that can later be converted to string when making containerPath
set locationName to "stuff you left out" --Just a name I assume...
-- get file container --
tell application "Finder"
set containerPath to ((container of filepathalias) as string)
end tell
-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"
-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
write listTextFinal to myFile as text
close access myFile
on error
close access myFile
end try
This works perfectly, if you were to be working to the desktop. The problem appears to be in the stage of getting the path correct.
Without all the massaging to the filepathalias I did in the first line we get the network file error. The file is trying to save in places you can not save to....
You will need to verify the filepathalias, containerPath, & finalFile all contain the information you'd expect.
Right below where the finalFile is set try this from the editor:
return {filepathalias as string, containerPath as string, finalFile as string}
my result from the above:
{"mac:Users:lithodora:Desktop:test:", "mac:Users:lithodora:Desktop:", "stuff you left out_RMP_2014.txt"}
That is similar to what you should expect.

-- Convert the file to a string
set desktop_path to POSIX path of (path to desktop)
set theFile to desktop_path & "subject_lines.csv" as POSIX file
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
write "Hello Kitty" to theOpenedFile
-- Close the file
close access theOpenedFile
this would cause the error "File file Mac:Users:jun:Desktop:subject_lines.csv is already open."
If delete "set theFile to theFile as string", and makes the following changes:
set theFile to desktop_path & "subject_lines.csv" as string
then the error "Network file permission error."

Related

How to change new File method in Groovy?

How do I replace the new File method with a secure one? Is it possible to create a python script and connect it?
Part of the code where I have a problem:
def template Name = new File(file: "${template}").normalize.name.replace(".html", "").replace(".yaml", "")
But when I run my pipeline, I get the error
java.lang.SecurityException: Unable to find constructor: new java.io .File java.util.LinkedHashMap
This method is prohibited and is blacklisted. How do I replace it and with what?
If you're reading the contents of the file, you can replace that "new File" with "readFile".
See https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#readfile-read-file-from-workspace
readFile: Read file from workspace
Reads a file from a relative path (with root in current directory, usually > workspace) and returns its content as a plain string.
file : String
Relative (/-separated) path to file within a workspace to read.
encoding : String (optional)
The encoding to use when reading the file. If left blank, the platform default encoding will be used. Binary files can be read into a Base64-encoded string by specifying "Base64" as the encoding.

Set Owner+Access-rights with io.open

In a lua-script (for Domoticz # Raspberry) I apply the following script-segment to generate an htm-file and to put it in the designated folder.
Line02text till Line30text are variables which are dynamically filled elsewhere in the lua-script.
file = io.open("/home/pi/domoticz/scripts/lua/XXXXX.htm", "w+")
-- Opens a file named XXXXX.htm (stored under the designated sub-folder of Domoticz)
-- in append mode
-- write lines to opened file
file:write("SOF<br>")
file:write(Line02text .. "<br>")
file:write(Line03text .. "<br>")
....
file:write(Line29text .. "<br>")
file:write(Line30text .. "<br>")
file:write("EOF<br>")
file:close() -- closes the open file
All seems OK, because the htm-file appears as planned.
Next steps would be to copy the file to different folder, open in browser, etc..
But Owner of the htm-file is 'root' and Permission is 0640.
For further application Owner should be different, and Permission e.g. 777.
Trying manual change or use of chmod results in report 'Permission denied' by server.
Question:
How to set (as result of the lua-script) different Owner and other Permission for the htm-file?
Lua's target is to be as portable as possible, and ownership/permissions management is very os-specific. There's no embedded functions to handle that.
You'll need to expose some native function that will do what you need with files' permissions. Or use some already existing library for that, like maybe lua-fs: (https://github.com/clementfarabet/lua-fs-0.3)

Applescript Finder path into POSIX path

I'm running the following applescript:
tell application "Finder"
set input to POSIX file "/Users/sam/Desktop/Resized"
set theFiles to (every file of folder input whose name does not contain "- Resized")
end tell
return theFiles
It's working as it should, though it's returning:
{document file "HighResCat.jpg" of folder "Resized" of folder "Desktop" of folder "sam" of folder "Users" of startup disk of application "Finder", document file "madonna.jpg" of folder "Resized" of folder "Desktop" of folder "sam" of folder "Users" of startup disk of application "Finder"}
where I need a POSIX path (/Users/sam/Desktop/Resized/HighResCat.jpg) to pass to automator
++++++++++++ EDIT
I've got it this far, but I can only pass one item of the list at a time when I need all of the items.
tell application "Finder"
set input to POSIX file "/Users/sam/Desktop/Resized"
set theFiles to (every file of folder input whose name does not contain "- Resized")
set input to item 1 of theFiles as string
end tell
return (POSIX file input)
I converted to a string and on return converted to POSIX
+++++++++EDIT2
This script worked inside automator:
on run {input, parameters}
set input to (input) as string
tell application "System Events" to set theFiles to POSIX path of (files of folder input whose name does not contain "- Resized")
set input to theFiles
return input
end run
thanks
Try:
set input to "/Users/sam/Desktop/Resized"
tell application "System Events" to set theFiles to POSIX path of (files of folder input whose name does not contain "- Resized")
return theFiles

Store Path as String Applescript

In my Applescript, I'm choosing a file of which I would like to store the path (to be opened later).
When I tried storing the path of the file as a string, this is the error I received:
error "Can’t make path of alias \"Macintosh
HD:Users:Username:Desktop:Folder:File.xls\" into type string." number -1700
from path of alias "Macintosh HD:Users:Username:Desktop:Folder:File.xls" to string
How can I effectively store this file path, so that I may recall it later when opening this file?
Applescript:
tell application "Finder"
set filePath to path of (choose file) as string
set fileName to name of file filePath
end tell
*Note: I also tried as text.
This seems to work:
tell application "Finder"
set filePathAlias to (choose file)
set fileName to name of filePathAlias
set filePath to filePathAlias as string
end tell

Save current document as .html with same name and path

I'm working on a script for FoldingText which will convert a FoldingText outline (basically a Markdown Text file) into a Remark presentation (an HTML script which makes slideshows from Markdown files). The script works, but I'd like to make the following improvement:
Instead of asking for the name and location to save the HTML file, I'd like to grab the name of the current document and save it as an HTML file (with the same name) in the current folder. The script should fail nicely if there is already a document with that name (offering to either write-over the document or save as a new document with a different name).
The script I'm using for writing to the file was from these forums. The first part is:
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as «class utf8»
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
And the second part is:
set theFile to choose file name with prompt "Set file name and location:"
my write_to_file(finalText, theFile, true)
Thanks.
FoldingText should have some way of retrieveng the path of the document. I've not found any free dowonload fo the application, so I've not benn able to check by myself, but you should be able to find it if you view the dictionary of the application.
My guess is that there's a property like 'path of', or 'file of' for the FoldingText document:
You will probably end up with something like this:
set theDocPath to file of front document of application "FoldingText"
tell application "Finder"
set theContainer to container of theFile
end tell
set newPath to (theContainer & "export.html) as string
repeat while (file newPath exists)
set newPath to choose file name with prompt "Set file name and location:"
end repeat
my write_to_file(finalText, newPath, true)

Resources