Applescript Finder path into POSIX path - 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

Related

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

Applescript: Moving a file using only it's path

Thanks to help from people on Stack Overflow, I'm working on a huge program using Applescript, but I have gotten stuck at a part where I have made a new folder, and I am trying to move 2 illustrator files into it. Normally this wouldn't be a problem as I have moved file before after selected them with "choose", but this time I'm not selecting them I only have variables with their full paths.
So my question is how can I move the 2 files using their full paths?
Can I somehow manipulate the variables into a more manageable format?
This is the part of the code not working (the full thing is huge but I can post it if requested) and a few snippets of where variables got their values from
set saveLocation to ((path to desktop) as string) --place to save the files
set theFile to choose file --choose .ai file to get outlines on
tell application "Finder" to set fileName to name of theFile
set fullPath to (saveLocation & fileName) --file path of new .ai
make new folder at saveLocation with properties {name:(text 1 thru ((length of fileName) - 3) of fileName)}
tell current application
set newFolder to POSIX path of saveLocation
set newFolder to newFolder & (text 1 thru ((length of fileName) - 3) of fileName)
*--set newFolder to choose folder* -- I tested to see if manually selecting the new folder helped, it didn't
move fullPath to newFolder
I really appreciate any help, thanks in advance
Very simple, set the make folder to a variable to call later. Looks like this:
tell app "finder" --need to use Finder to move files
set somevariable to make new folder at saveLocation with properties {name:(text 1 thru ((length of fileName) - 3) of fileName)}
move file fullPath to folder somevariable
end tell

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

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