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
Related
I've got a folder being generated with today's date with another AppleScript script, and I'm now wanting to copy that folder with today's date to another hard drive. I'm very new to AppleScript and am trying to figure out how to copy a folder from one hard drive to another, given the file name changes every time the day changes.
The code I'm using to create the folder with a specific date is below.
tell application "System Events"
delay 0.5
do shell script "date +\"%m.%d.%y\""
keystroke result
delay 0.5
keystroke return
end tell
This script should address your basic request. It gets the current date and builds a source and destination path with it. It then uses the ditto command to copy the contents of the source directory to the corresponding destination directory. If the destination does not yet exist, it will create it. This should include any sub-directories as well.
use scripting additions
-- do shell script "date +\"%m.%d.%y\""
set date8 to do shell script "date +\"%Y.%m.%d\""
set srcRoot1 to path to desktop as text
set srcRoot2 to POSIX path of srcRoot1
--> /Users/username/Desktop/2022.10.04/
set destRoot1 to path to documents folder as text
set destRoot2 to POSIX path of destRoot1
--> /Users/username/Documents/2022.10.04
do shell script "ditto -V " & quoted form of (srcRoot2 & date8 & "/") & space & quoted form of (destRoot2 & date8) & " 2>&1"
--> "ditto -V '/Users/username/Desktop/2022.10.04/' '/Users/username/Documents/2022.10.04' 2>&1"
The results above show the shell command after it is resolved. There are two optional components here. The -V option prints a list of copied files to stderr. The closing 2>&1 sends stderr to the script result where it can be seen.
I've been trying to find a program or some sort of app to run all the time and check one folder. If any document would be saved in the folder it would rename it like file1.png the next one file2.png.
Transnomino has an automation feature that allows to automatically rename any file dropped in a folder using a specified renaming Recipe. The Apple Script that does this looks something like this:
on adding folder items to theAttachedFolder after receiving theNewItems
tell application "Finder" to set thePath to POSIX path of theAttachedFolder
return do shell script ¬
"open -a Transnomino --args -d \"" & thePath & "\" -r \"" & thePath & ".recipe\""
end adding folder items to
A complete guide how this can be setup is described on the website of Transnomino. Here: https://transnomino.bastiaanverreijt.com/automation/index.html
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
I am currently setting up an ApplescriptObjC application. Whenever I try other methods, it screws up. I'm trying to set it up where a shell script uses the mv command to move a file from the "Files" directory to the /usr/bin/ folder. I think it would go a little something like: do shell script "sudo mv " & path & "/Files/ /usr/bin/" where path would be the path to me. I have tried the path to me and posix and other stuff, just doesn't work. ![The Files folder contains a file I want to move to /usr/bin] Image of where the folder is: http://i.stack.imgur.com/jUX6w.png
First of all the folder Files in your screenshot is a virtual folder in Xcode (yellow).
You have to create a real folder (blue). The easiest way is to drag a folder in Finder to the Xcode sidebar and select "Create folder references"
To use sudo in AppleScript append with administrator privileges to the do shell script line. You will be prompted to enter an admin password.
This code moves(!) the files to /usr/bin. If you want to copy (duplicate) the files use cp -r instead of mv
set filesFolder to (current application's NSBundle's mainBundle()'s resourcePath()'s stringByAppendingPathComponent:"Languages") as text
tell application "System Events" to set filesToMove to name of files of folder filesFolder
repeat with aFile in filesToMove
do shell script "/bin/mv " & quoted form of (filesFolder & "/" & aFile) & space & "/usr/bin/" with administrator privileges
end repeat
tell application "Finder"
set deletedfile to alias "Snow Leopard:Users:test.pdf"
delete deletedfile
end tell
The problem is I repeatedly call this script from my Cocoa application so the sound is played repeatedly too. Is it possible to disable that sound ?
Since the trash is just an invisible folder inside your home folder you can do this...
set myFile to (path to desktop folder as text) & "myFile.txt"
set trashFolder to path to trash folder from user domain
do shell script "mv " & quoted form of POSIX path of myFile & space & quoted form of POSIX path of trashFolder
one simple way (doesn't move to Trash)
do shell script "rm '/Users/test.pdf'"