do shell script "mdfind kMDItemCFBundleIdentifier = com.test.sample > ~/Documents/sampleBuilds.txt"
set homeFolder to (path to home folder)
set sampleFiles to paragraphs of (read POSIX file (POSIX path of homeFolder & "/Documents/sampleBuilds.txt"))
if (count of sampleFiles) is 0 then
display notification "No Sample builds are present on the system"
else
set sampleFiles to paragraphs of (read POSIX file (POSIX path of homeFolder & "/Documents/sampleBuilds.txt"))
display dialog "You have " & (count of sampleFiles) & " build(s) of Sample on your system. Would you like to remove them all? This cannot be undone"
repeat with c in SampleFiles
do shell script "rm -rf " & (quoted form of c)
display notification "Sample build located at " & c & " has been removed from your system"
end repeat
do shell script "rm -rf ~/Documents/SampleBuilds.txt"
end if
When placing script into Xcode with correct sender I get the error:
[AppDelegate testScript:]: Can’t make current application into type file. (error -1700) on the line
set sampleFiles to paragraphs of (read POSIX file (POSIX path of homeFolder & "/Documents/sampleBuilds.txt"))
Any thoughts on how to get Xcode in AppleScript Application to accept this script?
The coercion to POSIX file is not needed which solves the issue.
set sampleFiles to paragraphs of (read (POSIX path of homeFolder & "/Documents/sampleBuilds.txt"))
Basically in AppleScriptObjC POSIX file foo doesn't work, you have to write foo as POSIX file.
Side note:
Reading the text twice is redundant. You can delete the second set sampleFiles to ... line.
Related
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.
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 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
I have this script set up or rename my torrent folders, however it keeps giving me that weird finder error. It works when I don't read it from a text file, but I need that so that I don't need to select the folder all the time: Please help:
set read_folder to read (POSIX path of "/.torrentcleanup-prefs.txt") as text
set autofolder to (POSIX file read_folder)
set folderlist to ("")
tell application "Finder" to set folderlist to (get name of folders of folder autofolder)
repeat with i in folderlist
set dfilepath to (POSIX path of ((autofolder & i) as text))
set dfoldername to quoted form of POSIX path of dfilepath
set dfolder to i
set dmovie to quoted form of (text 1 thru ((length of dfolder) - 7) of dfolder as text)
try
do shell script "cd " & dfoldername & "; mv ./*.mkv ../" & dmovie & ".mkv"
end try
try
do shell script "cd " & dfoldername & "; mv ./*.mp4 ../" & dmovie & ".mp4"
end try
do shell script "rm -r " & dfoldername & ""
end repeat
By the Way, the contents of "/.torrentcleanup-prefs.txt" is:
/Users/student/Desktop/FIX ME NOW/Test Folder/
I didn't look at your whole script but you're reading the file wrong in the first line. You need to do it like this. So replace your first line with these two...
set myFile to "/.torrentcleanup-prefs.txt"
set read_folder to read (POSIX file myFile)