How to join Applescript paths? - applescript

Is there a more direct way to join paths together than to convert to and from a POSIX path in Applescript? For example:
POSIX file (POSIX path of (path to desktop folder) & "hello.txt")

You can concatenate HFS paths (colon separated) this way
(path to desktop as text) & "hello.txt"
path to desktop returns an alias specifier.
The parameter as text coerces the alias specifier to HFS path

Related

AppleScript not complying due to image

I'm using below code to load an image:
alias ((path to me) & "Contents:Resources:FCPXporter.icns" as string))
But I'm getting the error:
error "File alias Macintosh HD:Users:apple:Downloads:FCPXporter_Version_3.1.scptContents:Resources:FCPXporter.icns of «script» wasn’t found." number -43
How do I fix it?
The recommended syntax is
alias ((path to me as string) & "Contents:Resources:FCPXporter.icns")
It handles the path separators reliably.
But your script doesn't have a Resources folder because it has been saved as regular compiled script.
Two possible solutions:
Save the file as script bundle (.scptd), put the icon in the – now present – Resources folder and use the code above.
If the icon is supposed to be on the same level as the script use
set myself to path to me
tell application "System Events" to set parentFolder to (path of container of myself)
set theImage to (parentFolder & "FCPXporter.icns") as alias
In your code, your script file has the extension ".scpt" but your script file needs to be saved as either a script bundle (.scptd) or an application (.app) to have a "Resources" folder
set theImage to (path to me as string) & "Contents:Resources:FCPXporter.icns" as alias
-- Returns value.. alias "Macintosh HD:Users:apple:Downloads:FCPXporter_Version_3.1.scptd:Contents:Resources:FCPXporter.icns"
It looks like you are missing a path separator - try changing:
alias ((path to me) & "Contents:Resources:FCPXporter.icns" as string))
to
alias ((path to me) & ":Contents:Resources:FCPXporter.icns" as string))
If that still doesn't work then check that the file actually exists at the specified location - go to Terminal and:
ls "Macintosh HD/Users/apple/Downloads/FCPXporter_Version_3.1.scpt/Contents/Resources/FCPXporter.icns"

Two file path types in one command?

I have been struggling to make a basic program that copies selected files to a predetermined location. However it always ends up with the command having two different path types. is there any way that I can bypass this as it is bugging me and i have finished every other aspect of the program.
set targetFolder to (POSIX path of (path to home folder)) & "Library/Application Support/..." as POSIX file
set filepath to POSIX path of (choose file with prompt "Chose your file")
delay
do shell script "cp " & filepath & space & targetFolder
delay
display dialog "Your file has been moved!"
It's exactly the same as your previous question: Didn't you read my answer? You have to use quoted POSIX paths.
set applicationSupportFolder to POSIX path of (path to application support folder from user domain)
set filepath to POSIX path of (choose file with prompt "Chose your file")
do shell script "cp " & quoted form of filepath & space & quoted form of applicationSupportFolder
And you don't need any delays.

Set command in the middle of do shell script

I have been attempting to make a simple application that copies a selected file to a predetermined directory, only the variable does not work when set in a shell script. I was wondering if there is a way to phrase the copy or move command to allow it to take effect. The script is below.
set filepath to POSIX path of (choose file with prompt "Chose your file")
delay
do shell script "cp " & filepath & " ~/Library/Application Support/"
It does not work because space characters in paths must be escaped, the most reliable way is to use quoted form of.
path to application support folder from user domain is the relative path to the current user's application support folder.
set applicationSupportFolder to POSIX path of (path to application support folder from user domain)
set filepath to POSIX path of (choose file with prompt "Chose your file")
do shell script "cp " & quoted form of filepath & space & quoted form of applicationSupportFolder

Applescript creating hidden directory and transferring files

I was wondering why this script of mine was incorrect. (Note: I am new to AppleScript, so please ignore how terrible it is. :P)
set public to "~/Public/"
set p to POSIX path of public
tell application "Finder"
make new folder at p with properties {name:".folder"}
end tell
set sfolder to POSIX path of ((path to me as text) & "::")
set tfolder to "~/Public/.folder/"
duplicate files of sfolder to tfolder
It says there is an error at
make new folder at p with properties {name:".folder"}
error "Finder got an error: AppleEvent handler failed." number -10000
What should I do?
First of all, AppleScript cannot expand a tilde.
Second of all, the Finder doesn't accept POSIX paths.
Third of all, the duplicatecommand must be in a Finder application tell block.
Fourth of all, while the Finder is able to create the invisible folder it cannot copy the files because invisible files are only considered if "Show Invisible Files" is set to true in the preference file which is not reliable.
I recommend to use the shell for the whole task, ditto can duplicate the files and create intermediate directories simultaneously.
POSIX path of (path to public folder) is the same as ~/Public but returns the full path.
I don't know what the two colons stand for, so I just omitted them.
set publicSubFolder to POSIX path of (path to public folder) & ".folder"
set myself to POSIX path of (path to me)
do shell script "/usr/bin/ditto " & quoted form of myself & space & quoted form of publicSubFolder

Folder path of select file in AppleScript

I'm creating simple script to convert mp3 files using shell script. I decided to automate my conversion using applescript.
Basically what I'm doing is selecting mp3 file then splitting that file using my command line and i want to create a folder where the file is located (script will create that for me).
Now I just need to figure out how to get a path to a folder of the file.
How do I do that in applescript?
Here is the script that I have so far:
set mp3FileToSplit to choose file without invisibles
set thepath to mp3FileToSplit as text
set theposix to POSIX path of thepath
tell application "Finder" to set file_name to (name of mp3FileToSplit)
do shell script "/opt/local/bin/mp3splt -t 3.00 -d " & quoted form of file_name & " " & quoted form of theposix
Right now what that script does is creating folder on the root of my hard drive and I need to be in the folder where the file is located.
Any help will be appreciated.
tell application "Finder"
set f to POSIX file "/private/etc/" as alias
POSIX path of ((folder of f) as alias) -- /private/
end tell
Or
do shell script "dirname /private/etc/" -- /private

Resources