Applescript Variable issue - shell

I am having issues with this script when run it errors out
tell application "Finder"
set music_file to some file of the folder "Macintosh HD:ringtones"
end tell
set volume output volume 30
do shell script "afplay '/Volumes/Macintosh HD/ringtones/'" & music_file
this is the error:
do shell script "afplay '/Volumes/Macintosh HD/ringtones/'Macintosh HD:ringtones:Zen_ag_NARITA_HI_long_1.mp3"
--> error "may only specify one file to play
so I attempted to use a posix command such as:
set conver to POSIX file of music_file
and that errors out
What I cannot figure out is why the music_file is getting the whole HFS path in it when I tell it to just get the file and how to correct for it.

Try:
set myFolder to "/Users/me/Desktop/test"
tell application "System Events" to set music_file to POSIX path of (some file of folder myFolder)
set volume output volume 30
do shell script "afplay " & quoted form of music_file

AppleScript is weird. music_file is of type document file as defined by the Finder app, instead of being some other kind of file object. If you convert it to an alias with music_file as alias, then you can get the POSIX path of it which is what’s needed on the command line. Finally you need to wrap another set of ' characters around the filename in case it includes spaces. Note that this will break if the filename itself contains ' characters, as in Crazy 'Cause I Believe.mp3.
tell application "Finder"
set music_file to some file of the folder "ref:Users:andrew:Music:iTunes:iTunes Media:Ringtones"
end tell
set volume output volume 30
do shell script "afplay '" & (POSIX path of (music_file as alias)) & "'"

Related

AppleScript to append date to file

I was recently able to make a drag and drop script in Automator that allowed me to zip and name a file and then automatically apply the date (DDMMYY) but now it's defaulting to (DDMMYYYY) and I can't change it. I've googled for a solution and nothing works since this needs to be at the end of the file name.
Does anyone know what I'm doing wrong or does anyone have an actual script that can help me? Everything I've found only works if the date is at the start of the file name, not at the end (but before the extension).
You can use this AppleScript inside a Run AppleScript action, it renames the archive inserting the date before the file extension
on run {input, parameters}
set thePath to POSIX path of (item 1 of input)
set currentDate to do shell script "date +%d%m%y"
set newPath to text 1 thru -5 of thePath & "_" & currentDate & ".zip"
do shell script "mv " & quoted form of thePath & space & quoted form of newPath
return {POSIX file newPath as alias}
end run
Since you didn't provide any code I can't guess how you name your files, but the way to get DDMMYY is to use shell with the do shell script command:
tell application "Finder"
set theFile to (choose file)
set theName to name of theFile
set name of theFile to theName & "_" & (do shell script "date +%d%m%y") & ".xxx"
end tell
This doesn't get rid of the file extension of the original file. For that to work you would have to use something like this:
tell application "Finder"
set theFile to (choose file)
set theName to name of theFile
set periodIndex to offset of "." in theName
set theName to text 1 thru (periodIndex - 1) of theName
set name of theFile to theName & "_" & (do shell script "date +%d%m%y") & ".xxx"
end tell
The code responsible for removing the file extension comes from this post.
You can add punctuation how you like by putting the desired symbols before the next %. So date +%d.%m.%y is possible and improves readability.

How to run Applescript Photoshop Automate Batch of an Action

I am trying to make an Applescript that will launch Photoshop and call the Automate Batch function to run a specific Action. I have zero experience doing this and have only gotten snippets of code from my searching. I was wondering if someone could help me on this.. in particular if it is at all possible to pass a source folder to the batch call and then how to make the Batch call.
In particular, I am having issues trying to figure out how to:
Pass the source folder into the Batch Options
Call a specific Action in the Batch_Options from my Photoshop
Run the batch call with these options
I've updated with the latest code that is partially there...
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "Save as Photoshop PDF"
activate
try
set Batch_Options to {class:batch options, destination:save and close, error file:Error_File, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
batch "Save" from files folderList from action_set_name with options Batch_Options
end try
end tell
output:
"File Names are: Macintosh HD:Users:Joe:Desktop:Temp:Creature01_CO_v003.psdMacintosh HD:Users:Joe:Desktop:Temp:SecretLaboratory.psd"
Photoshop opens, and then nothing happens...
The following seems to work in the Automator:
tell application "Finder"
set sourceFolder to "Macintosh HD:Users:Joe:Desktop:Temp:" as alias
set folderList to every item of folder sourceFolder as alias list
do shell script "echo File Names are: " & folderList
#Alias list needs to be converted to a string for it to work in Photoshop batch call
set FileNames to {}
repeat with i from 1 to the count of folderList
set current_file to item i of folderList
copy current_file as text to the end of FileNames
end repeat
#Setup error log
set err_log to (sourceFolder as string) & "Error_Log.txt"
if not (exists file err_log) then
make new file at sourceFolder with properties {name:"Error_Log.txt"}
end if
end tell
tell application "Adobe Photoshop CC 2018"
set action_set_name to "SetA"
activate
set Batch_Options to {class:batch options, destination:save and close, error file:err_log, file naming:{document name lower, extension lower}, macintosh compatible:true, override open:false, override save:true, suppress open:true, suppressprofile:true, unix compatible:true, windows compatible:true}
#First arg is the Action Name, second is the list of files, third is the Action Set Name
batch "ActionA" from files FileNames from "SetA" with options Batch_Options
end tell

POSIX File Error - Extraneous double quotes?

This applescript works
set myFile to (POSIX path "/Users/fred/Documents/data.dat")
This applescript doesn't work
set myFileName to "/Users/fred/Documents/data.dat"
set myFile to (POSIX path myFileName)
It fails with the error
get POSIX file "/Users/fred/Documents/data.dat"
--> error number -1728
Result:
error "iTunes got an error: Can’t get POSIX file \"/Users/fred/Documents/data.dat\"." number -1728 from file "Macintosh HD:Users:drew:Desktop:Music:DIY:DIY-01.mp3"
It looks as if when using the variable, POSIX path is including the double quotes as explicit characters in the file name. What am I doing wrong?
The script below reproduces the problem.
tell application "Finder"
set newFileName to "/Users"
set newFile to POSIX file newFileName
end tell
Thanks
OK - I've found out what I should be doing.
The script below works - you just need to coerce the variable rather than pass it to POSIX file
tell application "Finder"
set newFileName to "/Users"
set newFile to (newFileName as POSIX file)
end tell
Result...
file "Macintosh HD:Users"
Thanks for your assistance.
Andrew
"/Users/fred/Documents/data.dat" Is already a posix path
tell application "Finder" to open POSIX file "/Users/fred/Documents/data.dat"
or
tell application "System Events" to open "/Users/fred/Documents/data.dat"
Here is an example for iTunes:
tell application "Finder" to set myFile to (POSIX file "/Users/John/Desktop/08 5150.mp3")
tell application "iTunes" to set resultTrack to add myFile to playlist "test"

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

On MAC O.S., how to get the name of files that has been dropped onto apple script to boot JAR with the name of file as a argument to JAR

In Script editor i have written a command to boot the JAR.
do shell script "cd /Desktop/RunJar/; java -jar RunMyJar.jar"
and Saved as script file as a Application. When i click on script file jar get run.
My requirement
I would like get the name of file that has been dropped onto the script file and would like to pass the name of that dropped file as an argument to my jar.
I have implemented this in Windows but could not working similar on MAC O.S.
On Windows
I have placed a BAT file to Boot JAR along with the absolute file name, that has been dropped on the bat file on windows. #echo %* will give me the list of files that has been dropped onto Batch file.
#echo %*
#pause
java -jar RunMyJar.jar %*
Similar i would like to implement in MAC O.S.
Thanks.
I found the following example at: Ben's AppleScript Snippets:
on open of finderObjects -- "open" handler triggered by drag'n'drop launches
repeat with i in (finderObjects) -- in case multiple objects dropped on applet
displayName(i) -- show file/folder's info
if folder of (info for i) is true then -- process folder's contents too
tell application "Finder" to set temp to (entire contents of i)
repeat with j in (temp)
display dialog j as string -- example of doing something with each item
end repeat
end if
end repeat
end open
You can also easily modify my answer to a similar question:
on open of theFiles -- Executed when files are dropped on the script
set fileCount to (get count of items in theFiles)
repeat with thisFile from 1 to fileCount
set theFile to item thisFile of theFiles
set theFileAlias to theFile as alias
tell application "Finder"
set fileInfo to info for theFileAlias
set fileName to name of fileInfo
-- something to this effect, but now that you have the file name,
-- do what you will...
do shell script "cd /Desktop/RunJar/; java -jar " & fileName
end tell
end repeat
end open
To add to Paul's answer
on open of finderObjects
repeat with currFile in finderObjects
-- ok, we have our current item. But it's an "alias": a Mac OS path
-- not a POSIX path
set unixPath to POSIX path of currFile
set base to do shell script "dirname " & unixPat
set fname to do shell script "basename " & unixPath
-- you could ask the Finder to give this to you too -- I'll use this way because
-- I'm guessing it's more familiar to you
do shell script "cd '" & base & "';" & " java -jar " & fname
end repeat
end open

Resources