How should an Apple Script look like to automatically open a file named testfile downloaded to the Downloads folder and run with a program testprogram?
I feel it should be a pretty simple template, and I am having trouble finding much to accomplish it.
This example uses Microsoft Word to open testFile.rtf. Update the code with your info and attach the folder action to your target folder.
on adding folder items to theFolder after receiving theFiles
set appPath to quoted form of (POSIX path of (path to applications folder) & "Microsoft Office 2011/Microsoft Word.app")
repeat with aFile in theFiles
tell application "Finder" to aFile's name = "testfile.rtf"
if the result then
set filePath to quoted form of aFile's POSIX path
do shell script "open -a " & appPath & space & filePath
end if
end repeat
end adding folder items to
If you manually want to open the file, try this:
set appPath to quoted form of (POSIX path of (path to applications folder) & "Microsoft Office 2011/Microsoft Word.app")
set filePath to quoted form of (POSIX path of (path to documents folder) & "testFile.rtf")
try
do shell script "open -a " & appPath & space & filePath
end try
You could create a folder action like this with Automator:
Finder's open command also has a using specifier for specifying the application:
tell application "Finder"
open POSIX file "/usr/share/doc/bash/bash.html" using (path to application "Safari")
end tell
Related
i use this script to create .md5 files for each file in a folder/subfolder.
But i wish to filter to only one filetype, for example .mp4
In each folder are a lot of files, but i need only the .md5 file for the filetype .mp4
Here is the apple-script:
set theFolder to (choose folder)
tell application "System Events"
set subfolders to every folder of theFolder
repeat with eachFolder in subfolders
set fPath to (POSIX path of eachFolder)
set fName to name of eachFolder
set md5Filename to quoted form of (fName & ".md5")
do shell script "cd " & quoted form of fPath & "; /sbin/md5 -r * > " & md5Filename
end repeat
end tell
Maybe, it´s very easy and one of you can help me.
Thanks.
Excluding filetypes in apple script.
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
could anyone tell me how I can create a simple installer of Kexts using AppleScrit in Xcode?
I've tried the following:
on installKext:sender
choose file
copy the result to thisFile
copy the (info for thisFile) to fileInfo
copy the name extension of the fileInfo to nameExtension
copy the file type of the fileInfo to filetype
tell application "Finder"
if the filetype is "Kext" or ¬
the nameExtension is "Kext" then
move thisFile to folder "Extensions" of folder "Library" of folder "System" of startup disk
end if
end tell
end installKext:
I Have this errors:
installKext:]: Not authorized to send Apple events to Finder. (error
-1743)
Error Domain=PlugInKit Code=13 "query cancelled"
UserInfo={NSLocalizedDescription=query cancelled}
You cannot move files with the Finder into folders which belong to the system.
You need root privileges for example with do shell script .... with administrator privileges
on installKext:sender
set theFile to choose file
tell application "System Events"
set {name extension:nameExtension, file type:fileType} to theFile
end tell
if fileType is "Kext" or nameExtension is "Kext" then
do shell script "mv " & quoted form of POSIX path of theFile & space & "/System/Library/Extensions/" with administrator privileges
end if
end installKext:
You must add the following in info.plist:
now everything works like a charm.
With the help of some users on this form I was able to create this script below which lets you choose a folder then choose which folders within the folder you want to rsync into a OneDrive Backup Folder.
Basically were migrating from a on site network storage to OneDrive for Business and want to create a script that as easy as possible for our users.
The issue I have is I dont want the users to be able to choose the original folder, I want to set the variable up front.
In the script I use:
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?
set OuserName to do shell script "whoami"
set userName to do shell script "echo " & OuserName & " | tr a-z A-Z"
tell application "Finder"
if not (disk userName exists) then
mount volume "SMB Server/" & userName & "$"
end if
delay 2
set theDialogText to "
- Mac H-Drive Migration Tool -
This Application Will Migrate a Copy of Your H Drive Data
to your OneDrive for Buisness Folder Locally on Your Mac
Migration Backup Location:
/Users/" & OuserName & "/OneDrive Folder/H-Drive Migration Backup
** Important **
In the Next Window Please Choose
The Root Folder of Your H Drive
The Drive Label Should Look Like: " & userName & "$"
display dialog theDialogText
set theFolder to (choose folder with prompt "Please Choose The Root of Your H Drive Or The Folder That Looks Like: " & userName & "$")
do shell script "mkdir -p ~/'OneDrive Folder'/'H-Drive Migration Backup'"
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
set AppName to "OneDrive.app"
tell application "Finder" to set Answer_ to exists application file ((path to applications folder as string) & AppName)
if Answer_ is false then
beep
beep
beep
beep
beep
end if
delay 1.5
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
set HDriveBackupFolder to ((path to home folder as text) & "OneDrive Folder:H-Drive Migration Backup")
end tell
repeat with thisName in theChosenNames
tell application "Terminal"
do script ("rsync -avpz --delete " & (quoted form of POSIX path of ((theFolder as text) & thisName)) & space & (quoted form of POSIX path of HDriveBackupFolder)
end tell
end repeat
end tell
You asked:
"How Do I set the theFolder for this script ahead of time instead of asking the user to pick the folder?"
Also, just prior to that, you said:
If I use:
set theFolder to "/Volumes/MYERSMI5$/"
I get "Can't Get every file of said folder" error message.
First, lets address setting the value of the theFolder variable. It should be as follows:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
I assume the error is being thrown at the following point in the code:
tell application "Finder"
activate
set theFolderNames to name of folders of theFolder
Change set theFolderNames to name of folders of theFolder to:
set theFolderNames to name of folders of container theFolder
To test this, I mounted a volume named MYERSMI5$ at /Volumes/ and created some folders within /Volumes/MYERSMI5$/. Then running the following code:
set theFolder to POSIX file "/Volumes/MYERSMI5$/"
tell application "Finder"
activate
set theFolderNames to name of folders of container theFolder
set theChosenNames to (choose from list theFolderNames with prompt "Choose Which Folders to Backup, Please Hold Down The ⌘ Key To Choose Multiple Folders " with multiple selections allowed)
if (theChosenNames is false) then return
end tell
It produced a list box, containing the names of the folders I created at that location, to choose from.
I did not try to run the entire block of code you included in your question, so if you're having other issues, you'll need to follow up after making the changes mentioned in my answer. There are good reasons why questions involving debugging code should conform to How to create a Minimal, Complete,and Verifiable example.
You may also want to review the Variables and Properties section in the AppleScript Language Guide.
I have looked around on the net for hours looking for this answer so I apologize if its there somewhere. This script below works fine except for the fact that it returns the .DS_Store file how can I exclude it from this query and all other hidden files for that matter but because I am creating the folder in my script the only one there is .DS_Store
tell application "System Events"
set listOfInputs to POSIX path of every disk item of folder a as list
end tell
Here is the full script below. I have been dropping files into the in folder after it is created but will eventually prompt for files before it is created.
on run
tell application "Finder"
if not (exists folder "IN") then
make new folder at desktop with properties {name:"IN"}
end if
if not (exists folder "OUT") then
make new folder at desktop with properties {name:"OUT"}
end if
end tell
set theINfolder to path to the desktop as string
set a to theINfolder & "IN:"
tell application "System Events"
set listOfInputs to POSIX path of every disk item of folder a as list
end tell
set inputs to listOfInputs
set theOutfolder to path to the desktop as string
set outputFolder to theOutfolder & "OUT:"
set params to {}
main(inputs, outputFolder, params)
end run
The following will work:
set listOfInputs to list folder a without invisibles
But without invisibles cannot be combined with POSIX path, so we use a loop instead:
set listOfInputs to {}
tell application "System Events"
set the_items to list folder a without invisibles
repeat with i from 1 to the count of the_items
set this_item to alias ((a as Unicode text) & (item i of the_items))
set end of listOfInputs to POSIX path of this_item
end repeat
end tell
The List Folder command has been deprecated and may stop working unexpectedly.
Try:
set inFolder to (path to desktop as text) & "IN"
do shell script "mkdir -p " & quoted form of (POSIX path of inFolder)
set listOfInputs to paragraphs 2 thru -1 of (do shell script ("find " & quoted form of (POSIX path of inFolder) & " \\! -name \".*\""))
You can get the result as a string separated by spaces like this:
set inFolder to (path to desktop as text) & "IN"
do shell script "mkdir -p " & quoted form of (POSIX path of inFolder)
set {TID, text item delimiters} to {text item delimiters, " "}
set listOfInputs to paragraphs 2 thru -1 of (do shell script ("find " & quoted form of (POSIX path of inFolder) & " \\! -name \".*\"")) as text
set text item delimiters to TID