Ideally I have these inputs:
set appId to "com.sourcegear.DiffMerge"
set path1 to "/tmp/file1.txt"
set path2 to "/tmp/file2.txt"
and need to run the app using appId with given arguments.
I know how to run the app using appId but that doesn't pass the args.
tell application id appId to activate
or I can run the app and pass the args but I don't know how to get the path from appId
set diff_path to "/Applications/pp/dev/DiffMerge.app/Contents/MacOS/DiffMerge"
set cmd to diff_path & " '" & path1 & "' '" & path2 & "'"
do shell script cmd
Do you know how to either ran activate with args or how to get full path from appId?
Not completely certain of your full intentions here but assuming that you wish to utilize diffmerge's command line capabilities in an applescript, try something like this:
set appID to "com.sourcegear.DiffMerge"
set appIDO to application id "com.sourcegear.DiffMerge"
set diff_path to quoted form of POSIX path of (path to appIDO) & "Contents/MacOS/DiffMerge"
set path1 to quoted form of "/tmp/file1.txt"
set path2 to quoted form of "/tmp/file2.txt"
set cmd to diff_path & space & path1 & space & path2
do shell script cmd
It will run this shell command:
"'/Applications/pp/dev/DiffMerge.app/Contents/MacOS/DiffMerge' '/tmp/file1.txt' '/tmp/file2.txt'"
The quoted form code wraps the text in single quotes so that there is no further interpretation of it by the shell. It's probably not an issue for your specific example but in case any of the paths or filenames gets a space, it shouldn't cause an error. The space does the obvious. If you wanted, you could compress the above into fewer lines.
For more information on how applescript handles 'do shell', look up an apple technical note TN2065 (it's easy to find). I should add that the activate command isn't part of this process. That command would make diffmerge the active application (and depending… might cause a window to open).
Update:
path to appID triggers the launching of the app and I'm not aware of any method of suppressing that.
It is possible to piece together the path to an app's CLI tool but it is not as general — specifically around nested folders in /Applications.
set sName to "DiffMerge"
set cName to "DiffMerge"
set aPath to path to (applications folder) as text
set sFol to ":Contents:MacOS:"
--> ":Contents:MacOS:"
set gApp to aPath & sName & ".app"
--> "MacHD:Applications:DiffMerge.app"
set cTool to aPath & gApp & sFol & cName
--> "MacHD:Applications:DiffMerge.app:Contents:MacOS:DiffMerge"
set cmdPath to quoted form of POSIX path of cTool
--> "'/Applications/DiffMerge.app/Contents/MacOS/DiffMerge'"
set path1 to quoted form of "/tmp/file1.txt"
set path2 to quoted form of "/tmp/file2.txt"
cmdPath & space & path1 & space & path2
--> "'/Applications/MacHD/Applications/DiffMerge.app/Contents/MacOS/DiffMerge' '/tmp/file1.txt' '/tmp/file2.txt'"
Related
I did the following and it works fine, I added my Command Line Tool to the Resource Folder of my project and did the following below:
set myPath to POSIX path of (path to resource "MyComandLineTool")
do shell script "cp " & quoted form of POSIX path of myPath & space & "/private/tmp" with administrator privileges
set cmd to "/private/tmp/myPath -r"
do shell script " " & cmd with administrator privileges
I wanted to know how not to pass it to /private/tmp, just run it from within the Resources folder of my project.
It's quite similar, just omit the copying part
set myPath to quoted form of (POSIX path of (path to resource "MyComandLineTool"))
do shell script myPath & " -r" with administrator privileges
Note: quoted form of is always good practice as the path could contain space characters
I'm currently trying to create an AppleScript Application which will copy a folder from its "Contents" folder to the user's desktop. The code I have at the moment is:
set theFile to path to me as string
set hfsme to theFile & "Contents:Folder"
set posixPath to POSIX path of hfsme
set contentfolder to posixPath
set AppleScript's text item delimiters to " "
set textItem to text items of contentfolder
set AppleScript's text item delimiters to "\\ "
set contentfolder to textItem as string
set AppleScript's text item delimiters to {""}
do shell script "cp -a " & contentfolder & " $HOME/Desktop"
However, I receive the following error upon run:
"cp: /Users/myusername/Desktop/Test Application.app/Contents/Folder: No such file or directory"
I think this is because I expect the copy location to be:
/Users/myusername/Desktop/Test\ Application.app/Contents/Folder
But the script fails to add the backslash before the space which is what the shell command requires.
My question is: how do I replace the spaces in the variable with "\ " ?
P.S. Before suggesting simply renaming the application a name which doesn't have a space, it is very important for me to have an application name with a space.
I have also tried using "duplicate" command with AppleScript but wasn't able to get this to work but I am open to suggestions!
(Please bear in mind that I'd like this script to work on other people's computers as well meaning that I cannot assume I already know the user's username.
Thank you in advance for any help!
Kind regards, Tom
Just add quoted form of, it handles the quotation in a very smart (and reliable) way.
set theFile to POSIX path of (path to me)
set contentfolder to theFile & "Contents/Folder"
do shell script "cp -a " & quoted form of contentfolder & " $HOME/Desktop"
If do shell script does not support $HOME write
do shell script "cp -a " & quoted form of contentfolder & space & quoted from of POSIX path of (path to desktop)
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.
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 trying to make a script that will take a screen shot, save the image to the desktop, and name it the date. Similar to what would happen if I used cmd + shift + 3. The only issue is the name of the image is only "Screen" instead of the entire name I specified. Anyone know how to fix this?
on run
set theDesktop to POSIX path of (path to desktop as string)
set theCurrentDate to current date
set shellCommand to "/usr/sbin/screencapture " & theDesktop & "Screen Shot" & theCurrentDate & ".png"
do shell script shellCommand
end run
The proper way to pass the path above is by using quoted form of:
on run
set theDesktop to POSIX path of (path to desktop as string)
set theCurrentDate to current date
set shellCommand to "/usr/sbin/screencapture " & quoted form of (theDesktop & "Screen Shot" & theCurrentDate & ".png")
do shell script shellCommand
end run
Put the complete file path in double quotes, like this:
on run
set theDesktop to POSIX path of (path to desktop as string)
set theCurrentDate to current date
set shellCommand to "/usr/sbin/screencapture \"" & theDesktop & "Screen Shot" & theCurrentDate & ".png\""
do shell script shellCommand
end run
The file name contains white space, hence, in your version, the command line interprets it as multiple arguments to /usr/sbin/screencapture.
I just use a shell command like this:
screencapture -i ~/Desktop/$(date +%Y%m%d%H%M%S).png
-i is interactive mode (like ⇧⌘4). The default file name format is like this on my installation:
date '+Screen Shot %Y-%m-%d at %-H.%M.%S %p.png'
See man screencapture and man strftime.
If you use AppleScript, the run handler is not needed, /usr/sbin/ is on the path by default, and you can escape arguments with quoted form of.
"Screen Shot " & (current date) & ".png"
do shell script "screencapture ~/Desktop/" & quoted form of result
If the file name looks like Screen Shot Wednesday, May 29, 2013 4/47/15 AM.png in Finder, it's because HFS uses colon as a pathname separator. : in shells appears as / in Finder and vice versa.
Emulate your keyboard:
-- Take Screenshot
tell application "System Events"
-- Shift-Command-3
key code 20 using {shift down, command down}
end tell
key code 20 is the same as number 3
Result:
Screen Shot 2018-11-18 at 12.47.39 pm
Complete list of key codes can be found here:
https://eastmanreference.com/complete-list-of-applescript-key-codes