在applescript中获取所选文件的名称

时间:2015-11-15 05:06:59

标签: applescript

我需要AppleScript知道用户在choose file命令中选择的文件的名称。这听起来应该很简单,但我无法弄清楚答案。该脚本从gif文件中提取帧,并将各个图像放在应用程序内容的文件夹中。然后它将桌面背景快速更改为文件夹内的图像,从而为您提供壁纸的GIF。但是,如果不知道所选gif文件的名称,我就无法做到这一点,因为我不知道文件夹中图像的名称。如果还有其他一些简单的方法,那也会很棒。这就是我到目前为止所做的:

on delay duration
set endTime to (current date) + duration
repeat while (current date) is less than endTime
    tell AppleScript to delay duration
end repeat
end delay   
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
set dest to quoted form of POSIX path of ((path to me as string) & "Contents:Resources:Gif")
set pScript to quoted form of "from AppKit import NSApplication, NSImage, NSImageCurrentFrame, NSGIFFileType; import sys, os
tName=os.path.basename(sys.argv[1])
dir=sys.argv[2]
app=NSApplication.sharedApplication() 
img=NSImage.alloc().initWithContentsOfFile_(sys.argv[1])
if img:
gifRep=img.representations()[0]
 frames=gifRep.valueForProperty_('NSImageFrameCount')
 if frames:
     for i in range(frames.intValue()):
         gifRep.setProperty_withValue_(NSImageCurrentFrame, i)
         gifRep.representationUsingType_properties_(NSGIFFileType, None).writeToFile_atomically_(dir + tName + ' ' + str(i + 1).zfill(2) + '.gif', True)
     print (i + 1)"
repeat with f in gifFiles
set numberOfExtractedGIFs to (do shell script "/usr/bin/python -c " & pScript & " " & (quoted form of POSIX path of f) & " " & dest) as integer
end repeat
repeat
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image
delay 0.05
set desktop_image to (path to me as string) & "Contents:Resources:Gif:"
tell application "Finder" to set the desktop picture to desktop_image

2 个答案:

答案 0 :(得分:0)

您可以使用System Events

获取名称
set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
tell application "System Events" to set gifFileName to name of gifFiles

或使用info for命令,虽然它已经被弃用了多年,但它仍然在El Capitan中工作

set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
set gifFileName to name of (info for gifFiles)

当然可以使用Finder,但建议尽可能避免使用Finder

答案 1 :(得分:0)

要获取文件名,您必须使用Finder名称属性:

set gifFiles to choose file of type "com.compuserve.gif" with prompt "Select GIF"
tell application "Finder" to set myName to name of file gifFiles

变量myName包含所选文件的名称(带扩展名)。