Get file Path from AppleScript - applescript

I need to get the file path of the chosen folder from AppleScript. I am using the following code to do this.
--Display selection window
set source_folder to choose folder with prompt "Please select directory."
--Get the path of the folder
set item_list to get the path of every disk item of source_folder
I am getting a file path like this:
File:Path:To:the:fodler
What I want is:
File/Path/To/the/folder

Try:
set source_folder to choose folder with prompt "Please select directory."
tell application "System Events"
set item_list to POSIX path of every disk item of source_folder
end tell

Try getting the POSIX path instead:
set item_list to get the POSIX path of every disk item of source_folder

Related

Apple Script: Copy file (being selected from prompt) to another location

I am struggling with my Apple Script. I am asking to select a file and the selected file needs to be copied to another location. I am new to Apple Scripting, so probably made some mistake. I tried different versions with "copy" instead of "duplicate" or "alias" instead of "file", but nothing worked so far. Hope, somebody can help me figure this out.
This is what I scripted so far (I get an AppleEvent timed out):
set DefaultPath to POSIX file "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Test"
set DestFolder to "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Destination"
set theFile to (choose file with prompt "Select file:" default location (DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell
The problem is that the Finder doesn't support POSIX paths.
I recommend to use a relative path path to library folder from user domain and HFS paths (colon separated)
To satisfy the location parameter of choose file put the alias keyword in front of the (HFS) path
set cloudDocs to (path to library folder from user domain as text) & "Mobile Documents:com~apple~CloudDocs:"
set DefaultPath to cloudDocs & "FOLDER:Test:"
set DestFolder to cloudDocs & "FOLDER:Destination:"
set theFile to (choose file with prompt "Select file:" default location (alias DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell

Hiding all files in a user chosen directory

So basically I want to be able to take all of the files in a folder and hide them but it always stops at this point with Applescript error -1700
tell application "Finder"
set filePath to entire contents of (choose folder with
prompt "Please choose your folder") as alias list
set pFilePath to POSIX path of filePath
end tell
Can you guys help me?

Check if file names are the same using Applescript

How would I go about checking if a file (an alias called theFile) has the same name as any other files in another folder?
I would like to have an Applescript look through all of the files in the folder and has a if and else as a result if it does match one of the names or doesn't.
Choose a folder
Get the name of theFile
Get a list of all file names in the folder
Check if the list contains the name of theFile
set theFile to alias ...
set theFolder to choose folder
tell application "System Events"
set theFileName to name of theFile
set otherFileNames to name of files of theFolder
end tell
set fileNameExists to otherFileNames contains theFileName

AppleScript: move files with System Events or bash

When using Finder, script can take a lot of time. I also want this script to work on the background. How can I use System Events/bash instead of Finder in this script?
property source_folder_one : alias "OS X:Users:username:Pictures:Work:New:one"
property source_folder_two : alias "OS X:Users:username:Pictures:Work:New:two"
property save_folder_one : alias "OS X:Users:username:Pictures:Work:Waitlist:one"
property save_folder_two : alias "OS X:Users:username:Pictures:Work:Waitlist:two"
tell application "Finder"
move entire contents of folder source_folder_one to folder save_folder_one
move entire contents of folder source_folder_two to folder save_folder_two
end tell
display notification "All images were relocated." with title "Relocating Complete" sound name "Glass.aiff"
tell me to quit
I made an applescript which does what you want.
But for one folder instead of two, you can choose the folder in an prompt.
set sourceFolder to (choose folder) as alias
set saveFolder to (choose folder) as alias
set sourceFolderPOSIXPath to (the POSIX path of sourceFolder)
set saveFolderPOSIXPath to (the POSIX path of saveFolder)
set shellString to "mv " & sourceFolderPOSIXPath & "* " & saveFolderPOSIXPath
do shell script shellString
display notification "All files were relocated." with title "Relocating Complete" sound name "Glass.aiff"
If you want to use your constant alias, just replace (choose folder) with "OS X:Users:username:Pictures:Work:New:one" and so on.

Applescript rename file in current folder

I want to change a filename in the folder which is not always the same, it depends on where the applescript is stored (same folder as the file to change).
I made this script with a dialog to check the path, that works fine but after but I get an error (-1700, Can't change "test" into an integer. Why, and how do I fix this?
tell application "Finder"
set thePath to POSIX path of ((path to me as string) & "::")
display dialog thePath buttons {"Clipboard", "OK"} default button 2
if the button returned of the result is "Clipboard" then
set the clipboard to thePath
end if
set name of document file "test" of thePath to "test_OLD"
end tell
If you are using Finder, which you only need for the set name statement, you need to coerce thePath from a posix path to a hfs path.
You can also remove the entire Finder block and use:
tell application "System Events" to set name of file (thePath & "test") to "test2"
Could this be an easier way syntax wise?
tell application "System Events"
set name of file "/Users/Firebird/Documents/test2.jpg" to "/Users/Firebird/Documents/11.jpg"
end tell

Resources