How to change Current Working directory using AppleScript - applescript

What would be a simplest way to change a current working directory (cwd) using AppleScript. It would be equivalent to OS's cd or Python's os.chdir. Would be great if a destination directory if doesn't exist would be created on a fly (but that would be highly optional).

If you are looking to use with an applications save dialog...
set filePath to "path/to/my/file"
tell application "System Events"
-- Once save dialog is open
keystroke "g" using {shift down, command down}
delay 1
set value of text field 1 of sheet 1 of sheet 1 of window 1 to filePath
end tell

You might be thinking of doing something like this:
tell application "Finder"
# ^^^ or whatever application you want to control up there
# to get to a file...
set filePath to POSIX file "/Users/username/Documents/new.mp3"
# to get to a folder...
set testFolder to POSIX path "/Users/username/test"
if (exists (folder testFolder)) then
say "folder exists"
else
make new folder at testFolder
endif
end tell
I'm basing my answer off the answers on this page and this related question (or this one).

Related

Using AppleScript to Select a File

I'm trying to get AppleScript to select a file, but I'm getting an error when I execute the script.
Here's the code
tell application "System Events"
set a to "/Users/me/files/"
set fileName to "myFile.jpg"
set thePath to POSIX path of a
tell application "Finder"
set selection to fileName of thePath
end tell
keystroke "c" using command down
end tell
I'm getting an error "Can’t get POSIX path of "/Users/me/files/"
Essentially, what I'm trying to do is find a way to select a file so that I can copy it for later. But I want to copy the actual file, not the path of the file. The idea is to create a service that copies the file so that I can paste it into another application easily.
If there's a better way to do this, then please let me know
These following 2 lines of code would copy your file to the clipboard. This would only work with a single file. Not multiple items.
activate
set the clipboard to POSIX file (POSIX path of (choose file))

Quicktime Automator + Applescript for QuickTime reference files

I'm trying to create an Automator droplet that allows me to pass one or more video files to an Applescript that then saves out reference files with "_ref.mov" appended to the file name by using QuickTime Player 7.
Saving as a reference is the default save method, which is why I'm not using save reference file function (also, because I can't find any docs on the using descriptors attribute object.)
I found this script on another forum, and it works, but it doesn't accomplish exactly what I need because it requires that 1) a file is open and 2) the user manually specifies an output file name.
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
if not (exists document 1) then display dialog "please open a quicktime movie." buttons {"cancel"} default button 1 with icon 1
set theFile to (choose file name)
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've tried to make changes to accomplish what I need, but my changes break the script:
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
open input
set theFile to ((path of input) & "_ref.mov))
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've also tried:
set theFile to (((path of input) & "_ref.mov") as text) doesn't work
either does as alias or as string
Not sure where I'm going wrong. Please help if you can!

Applescript - save argument to text file

I need to write a simple applescript that will be opened with an argument. That argument will be a file path. I need it to save this file path to a text file. I want to avoid using shell script.
I'm finding this surprisingly difficult, despite having done it in .sh and .bat already
Thanks very much!
Adam
//// the current code which is not working is below. the code begins with "on run" and ends with "end run" but for some reason this isn't being highlighted as code
on run argv
set this_PATH to (POSIX file argv)
tell application "TextEdit"
activate
make new document
set text of document 1 to this_PATH as text
save document 1 in "/Users/adamparkinson/Desktop/date.txt"
end tell
end run
I made this work:
#!/usr/bin/osascript
on run argv
tell application "TextEdit"
activate
make new document
tell document 1
set its text to (item 1 of argv as text)
save in posix file (item 1 of argv as text )
end tell
end tell
end run
I called it like this:
./SavePath.sh /Users/Me/Desktop/junk/Newstuff.txt (Of course I used chmod u+x SavePath.sh to make it executable.)
If this works for you, then please check of my answer as answered. :)
with applescript it is very easy to create application Drag and Drop without any script shell below a version Drag and Drop of your script
on open draggedItems
set this_PATH to quoted form of POSIX path of draggedItems
repeat with currentItem in draggedItems
tell application "TextEdit"
activate
set doc to open POSIX file "/Users/adamparkinson/Desktop/date.txt"
make new word at end of text of doc with data (this_PATH as text) & return
end tell
end repeat
end open

Tell application process that contain a word

This question may seem strange. However I'm in the need of interacting with an application that is releasing worldwide with different names, eg. AppEN, AppGB, AppDE etc...
I am looking for a solution that allow me to use this command:
tell application process "AppnameGB"
However it should work with all the different variations of this application. I don't know if this is possible but searching for a string in the application name could do the trick: tell application process that contain in its name "Appname".
If the process is already open, you can use something like this:
tell application "System Events"
tell (process 1 where name starts with "TextEd")
properties
set f to its file
end tell
end tell
tell application (f as text)
properties
end tell
Telling Finder to list files is really slow:
tell application "Finder"
item 1 of (path to applications folder) where name starts with "Text"
end tell
You can use do shell script though:
set a to do shell script "ls /Applications/ | grep -m1 '^Text.*\\.app$'"
tell application a
properties
end tell
set a to do shell script "mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName==Text*' | head -n1" would also search outside the applications folder.
The easiest is if you could use the bundle identifier of the application instead of its name. Most likely the bundle ID doesn't change while the name does. So use this script on that application and check if the bundle ID changes or not.
tell application "Finder" to set bundleID to id of (choose file)
If you find it doesn't change then you can access it via applescript like this...
tell application "com.bundle.id"
activate
-- do something
end tell
Your only other alternative is get a list of all applications, and loop through them checking the name as you suggest. Something like this wold work but is pretty slow.
-- use spotlight to find all the apps on the computer
set cmd to "mdfind 'kMDItemContentType == \"com.apple.application-bundle\"'"
set foundApps to paragraphs of (do shell script cmd)
-- search the found apps looking for the one you're interested in
set appPath to missing value
repeat with i from 1 to count of foundApps
if (item i of foundApps) contains "Appname" then
set appPath to item i of foundApps
exit repeat
end if
end repeat
if appPath is missing value then error "Couldn't find the app!"
-- open the app
set macAppPath to (POSIX file appPath) as text
tell application macAppPath
activate
-- do something
end tell

Using Automator or Applescript or both to recursively print documents to PDF

I have a massive set of files (4000+) that are in an old Apple format (Appleworks). My employed needs them all updated to PDF. By opening the documents in Appleworks and using the system print dialogue, I can save them to PDF—this is ideal. I'm a complete nub with Applescript/Automator, however.
Using a Python script I was able to gather all the Appleworks files from my bosses computer and put them in a directory; each file is then in a subdirectory with a .txt file containing its original location (where, eventually, I will have to put them back).
I need the script to move recursively through this massive directory, getting every file that's neither a folder nor a .txt document, and save it to PDF in the same directory in which the original file was found. ie.
/Appleworks/Boss_File_1/
will contain
/Appleworks/Boss_File_1/Boss_file_1.cwk and
/Appleworks/Boss_File_1/path.txt
But must eventually also contain /Appleworks/Boss_File_1/Boss_File_1.pdf
I can get half way with either solution, but don't know how to make them work together. The Applescript I'm using looks like:
set appleworksFolder to choose folder
tell application "Finder"
set folderItems to (files of entire contents of appleworksFolder)
repeat with I from 1 to number of items in folderItems
set the_doc to item I of folderItems
if name of the_doc is not "path.txt" then
try
tell application "AppleWorks 6"
open the_doc
tell application "System Events"
tell process "Appleworks"
keystroke "p" using command down
click menu button "PDF" of window "Print"
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
click button "Save" of window "Save"
end tell
end tell
end tell
end try
else
tell application "Finder"
delete the_doc
end tell
end if
end repeat
end tell`
This opens the print dialogue but never gets any further and I have no idea why. I realize this script also doesn't deal with putting the document back in its original folder, but in Applescript I could easily enough do this if I could get past the actual printing-to-PDF bit.
Meanwhile, in Automator, using this workflow:
Get Specified Finder Items
Get Folder Contents
Filter Finder Items (by kind and then by file extension is not .txt)
Open Finder Items (with Appleworks)
I then am stuck; using the actual Print Finder Items and choosing Adobe PDF seems to actually do nothing at all, and recording myself using the print to pdf process live is useless because I don't know how to get Automator to retain the path the file originated from and ensure it prints to it.
If anyone can help me put this together somehow, I'd be enormously grateful. Thanks.
Convert using Pages
If you have Pages (part of iWork), it can open .cwk files and save them as PDF: just replace your if block with this:
if (the_doc's name extension is not "txt") then
set newName to my makeNewFileName(the_doc, "pdf")
try
tell application "Pages"
open (the_doc as alias)
set thisDoc to front document
save thisDoc as "SLDocumentTypePDF" in newName
close thisDoc saving no
end tell
on error
display dialog "Error: cannot export " & (name of the_doc) & " to PDF."
end try
end if
(you will need this custom function makeNewFileName):
(* prepare new file name with extension ext *)
on makeNewFileName(finderItem, ext)
tell application "Finder"
set fname to finderItem's name
set thePath to (finderItem's container) as alias as text
return (thePath & (text 1 thru ((length of fname) - (length of (finderItem's name extension as text))) of fname) & ext)
end tell
end makeNewFileName
(complete working script)
GUI scripting
Alternatively, you could do GUI scripting upon AppleWorks as you attempted, but it has the disadvantage that you cannot programmatically specify where to save the PDF file.
This snippet works for me:
tell application "AppleWorks 6"
open the_doc
activate
tell application "System Events" to tell process "AppleWorks"
keystroke "p" using command down
delay 1 -- or longer, if it takes longer
click menu button "PDF" of window "Print"
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
delay 1 -- or longer
click button "Save" of window "Save"
end tell
end tell
Unfortunately, AppleWorks doesn't seem to properly listen to AppleScript's close command, therefore you may need to close the file by also simulating the cmd+W keystrokes.
Try this:
set appleworksFolder to choose folder
set thePath to POSIX path of appleworksFolder as string
tell application "Finder"
set folderItems to files of appleworksFolder
repeat with aFile in folderItems
set {name:fileName, name extension:nameExtension} to aFile
set filePath to POSIX path of (aFile as alias) as string
if nameExtension is not "txt" then
set theLocation to POSIX path of (aFile as text)
set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
set destLocation to (thePath & baseName & ".pdf")
set theCommand to "/System/Library/Printers/Libraries/./convert -f \"" & filePath & "\"" & " -o " & "\"" & destLocation & "\"" & " -j \"application/pdf\""
do shell script theCommand
else
tell application "Finder" to delete aFile
end if
end repeat
end tell
I needed to do this today on Mountain Lion with a bunch of RTF receipts; here's how I did it:
#!/bin/bash
for file in *.rtf ; do
filename=$(basename "$file")
/usr/sbin/cupsfilter "$file" > "$filename.pdf"
done
Worked great; super easy. No Automator or AppleScript silliness.

Resources