Store Path as String Applescript - path

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

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.

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

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."

In automator, using applescript, how can I append a folder name to a selected path correctly?

This automator script is an example. When i display the path it looks correct, but if i then try to copy files to that path I get an error.
this is the fix - needed posix paths.
on run {input, parameters}
set theFilePath to POSIX path of (input as string) & "/Samples" as string
display dialog theFilePath
return theFilePath
end run

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)

Can't set OBJECTS_DIR from .pri file

I want to have one directory for all object files and create Common.pri file that set OBJECTS_DIR like that
OBJECTS_DIR = $$PWD/../
But when build project i can't find obj file in given directory.If I write this direct in .pro file I get the expected result.I successfully include Common.pri file. I checked that with
!include( ../../Common.pri)::warning(Fail to include Common.pri)
How to achieve what i want.I can't find anything in google
The PWD variable specifies the full path leading to the directory containing the current file being parsed, that is, in your case the full path leading to the Common.pri file and NOT the .pro file. I would place a warning($$OBJECTS_DIR) function in both the .pri and the .pro file to verify the value of OBJECTS_DIR variable.

Resources